Powershell 将每行的字符串移动到特定位置

Powershell 将每行的字符串移动到特定位置,powershell,Powershell,在PowerShell中,如何将每行的字符串移动到特定位置 例如: CXTYPMORDER TEXT IS 'Type of order' , CXNUMORD TEXT IS 'Number of order' , CXCC TEXT IS 'Code of order' , CXDAY TEXT IS 'Day of order' , CXMONTH TEXT IS 'Month of order' , 通缉结果: CXTYPMORDER TEXT IS 'Type of o

在PowerShell中,如何将每行的字符串移动到特定位置

例如:

CXTYPMORDER TEXT IS 'Type of order' , 
CXNUMORD TEXT IS 'Number of order' , 
CXCC TEXT IS 'Code of order' , 
CXDAY TEXT IS 'Day of order' , 
CXMONTH TEXT IS 'Month of order' , 
通缉结果:

CXTYPMORDER   TEXT IS 'Type of order' , 
CXNUMORD      TEXT IS 'Number of order' , 
CXCC          TEXT IS 'Code of order' , 
CXDAY         TEXT IS 'Day of order' , 
CXMONTH       TEXT IS 'Month of order' , 
编辑1:

我试过这个:

    $newstreamreader = New-Object System.IO.StreamReader("N:\TEMP\TEST\TEST.txt")
    $newstreamwriter = New-Object System.IO.StreamWriter("N:\TEMP\TEST\TEST_New.txt")
    $eachlinenumber = 1
    $position = 0
    $numberofspace = 0
    
    while (($readeachline =$newstreamreader.ReadLine()) -ne $null)
    {
        if($readeachline -match "^(\S+) TEXT IS '([^']+)'"){
            $position = $readeachline.IndexOf("TEXT IS")
            $numberofspace = 15-$position
            $newstring = $readeachline.Insert($position, " " * $numberofspace)
            $newstreamwriter.WriteLine($newstring)
        }
        $eachlinenumber++
    }

$newstreamwriter.Close()
编辑2:

现在可以了:)
谢谢大家。

您可以使用
-match
正则表达式操作符匹配并捕获每行的相关部分,然后构造一个易于格式化的对象,以便于可读:

# Use Get-Content to read the file line by line
$Descriptions = Get-Content path\to\file.txt |ForEach-Object {
  # Check if current line is what we're looking for:
  if($_ -match "^(\S+) TEXT IS '([^']+)'"){
    # Extract capture group values
    $EntryName = $Matches[1]
    $Description = $Matches[2]

    # Create a new object with the extracted values
    [pscustomobject]@{
      Name = $EntryName
      Description = $Description
    }
  }
}
现在,您可以使用
格式表
使其成为一个可读的表格:

PS C:\> $Descriptions |Format-Table -AutoSize

Name        Description
----        -----------
CXTYPMORDER Type of order
CXNUMORD    Number of order
CXCC        Code of order
CXDAY       Day of order
CXMONTH     Month of order

上面使用的正则表达式模式(
^(\S+)文本为“([^']+)”
)描述:

^           # Start of string
(           # Start capture group
\S+         # 1 or more non-whitespace characters
)           # End capture group
 TEXT IS '  # Literal string ` TEXT IS '
(           # Start capture group
[^']+       # 1 or more characters that aren't single-quotes
)           # End capture group
'           # Literal single-quote 

您可以使用
-match
regex操作符匹配并捕获每行的相关部分,然后构造一个易于格式化的对象,以便于可读:

# Use Get-Content to read the file line by line
$Descriptions = Get-Content path\to\file.txt |ForEach-Object {
  # Check if current line is what we're looking for:
  if($_ -match "^(\S+) TEXT IS '([^']+)'"){
    # Extract capture group values
    $EntryName = $Matches[1]
    $Description = $Matches[2]

    # Create a new object with the extracted values
    [pscustomobject]@{
      Name = $EntryName
      Description = $Description
    }
  }
}
现在,您可以使用
格式表
使其成为一个可读的表格:

PS C:\> $Descriptions |Format-Table -AutoSize

Name        Description
----        -----------
CXTYPMORDER Type of order
CXNUMORD    Number of order
CXCC        Code of order
CXDAY       Day of order
CXMONTH     Month of order

上面使用的正则表达式模式(
^(\S+)文本为“([^']+)”
)描述:

^           # Start of string
(           # Start capture group
\S+         # 1 or more non-whitespace characters
)           # End capture group
 TEXT IS '  # Literal string ` TEXT IS '
(           # Start capture group
[^']+       # 1 or more characters that aren't single-quotes
)           # End capture group
'           # Literal single-quote 



到目前为止,您尝试了什么?请分享你的代码。。。你为什么要这么做?re可能是一种更好的方法对不起,我是PowerShell的新手,所以我不知道怎么做。它只是为了方便地读取一个大文件(74000行)。它是对数据库中每个表的描述。教你像Powershell这样的复杂技术超出了SO的范围。你必须先学习基础知识。所以不是免费的编码服务。你试过找它吗?关于如何使用Powershell操作字符串,可能有成千上万的例子。到目前为止,您尝试了什么?请分享你的代码。。。你为什么要这么做?re可能是一种更好的方法对不起,我是PowerShell的新手,所以我不知道怎么做。它只是为了方便地读取一个大文件(74000行)。它是对数据库中每个表的描述。教你像Powershell这样的复杂技术超出了SO的范围。你必须先学习基础知识。所以不是免费的编码服务。你试过找它吗?关于如何使用Powershell操作字符串,可能有成千上万的示例。谢谢,但我想做的是将更改写入文件。我将编辑我的问题,向您展示我的发现。@SalahK。完成后,您需要在流编写器上调用
Close()
,这将刷新其缓冲区并释放文件句柄(之后您将看到写入的数据)。谢谢,但我要做的是,将更改写入文件。我将编辑我的问题,向您展示我的发现。@SalahK。完成后,需要在流编写器上调用
Close()
,这将刷新其缓冲区并释放文件句柄(之后将看到写入的数据)