如何格式化非结构化文本文件?

时间:2013-07-09 08:45:26

标签: powershell

1111    700000000000c940  FIBRE  FA-11G:1,FA-12G:1
        700000000000c940  FIBRE  FA-11G:1,FA-12G:1
        700000000000c940  FIBRE  FA-11G:1,FA-12G:1
        700000000000c940  FIBRE  FA-11G:1,FA-12G:1
2211    700000000000c940  FIBRE  FA-11G:1,FA-12G:1
        700000000000c940  FIBRE  FA-11G:1,FA-12G:1
        700000000000c940  FIBRE  FA-11G:1,FA-12G:1
        700000000000c940  FIBRE  FA-11G:1,FA-12G:1
3311    700000000000c940  FIBRE  FA-11G:1,FA-12G:1
        700000000000c940  FIBRE  FA-11G:1,FA-12G:1
        700000000000c940  FIBRE  FA-11G:1,FA-12G:1
        700000000000c940  FIBRE  FA-11G:1,FA-12G:1

如何使用powershell在同一行上打印?

1111 700000000000c940 700000000000c940 700000000000c940 700000000000c940.
2211 700000000000c940 700000000000c940 700000000000c940 700000000000c940.
3311 700000000000c940 700000000000c940 700000000000c940 700000000000c940.

任何帮助都将深受赞赏

2 个答案:

答案 0 :(得分:0)

现在它更有意义了。看看我最喜欢的解析文本switch -regex的工具:

switch -regex -file foo.txt {
    '^(\d+)\s+(\S+)' {
        if ($s) { $s }
        $s = "$($Matches[1]) $($Matches[2])"
    }
    '^\s+(\S+)' { $s += " $($Matches[1])" }
}
if ($s) { $s }

答案 1 :(得分:0)

如果你有PowerShell v3,试试这个单行:

(Get-Content file.txt -Raw) -replace '(?<=\w{16}\s).+' -replace '(?<= ) |\r?\n\s+'