Powershell通过删除不需要的行来操作文件

时间:2016-02-17 06:30:55

标签: powershell

如何编写可以从以下格式操作文件的powershell脚本

AREA: MAC
SUB_AREA: MAB
AREA_CODE: MA1
SUB_AREA_CODE: MA11
YEAR: 2015
VERSION: 1

---------------------------------------------------------------------
|column1|column2|cloumn3.............|last column
|--------------------------------------------------------------------
|Data1|Data2.........................|last data
|Data1|Data2.........................|last data
---------------------------------------------------------------------
AREA: MACC
SUB_AREA: MABB
AREA_CODE: MAC1
SUB_AREA_CODE: MAB11
YEAR: 2015
VERSION: 1

---------------------------------------------------------------------
|column1|column2|cloumn3.............|last column
|--------------------------------------------------------------------
|Data1|Data2.........................|last data
|Data1|Data2.........................|last data
---------------------------------------------------------------------

AREA: MACC
SUB_AREA: MABB
AREA_CODE: MAC1
SUB_AREA_CODE: MAB11
YEAR: 2016
VERSION: 2

---------------------------------------------------------------------
|column1|column2|cloumn3.............|last column
|--------------------------------------------------------------------
|Data1|Data2.........................|last data
|Data1|Data2.........................|last data
|--------------------------------------------------------------------
|*| 
| |
----------------------------------------------------------------------- 

|column1|column2|cloumn3.............|last column
|Data1|Data2.........................|last data
|Data1|Data2.........................|last data

规则是: 我只需要文件中的第一个列标题,并将数据追加到数据末尾并从文件中删除最后四行。下面的示例截图。

原始文件

enter image description here

预期文件

enter image description here

这是代码,如何重写代码,以便在文件中排除重复的标头?我只希望列标题显示在该行的顶部,并且只显示一次。

$path = "D:\PowerShell\PC.TXT"
$outPath = "D:\PowerShell\Output\PC_Format.TXT"
$d = Get-Content -path $path
$d | Select-String -Pattern '^\|[^-]+' -allmatches |
Out-File -width 10000 -filepath $outPath

1 个答案:

答案 0 :(得分:0)

您似乎只对以管道字符|开头但后面没有短划线-的行感兴趣。如何通过简单匹配以管道和非破折号开头的行来过滤数据?像这样,

$d = get-content "c:\myDataFile.txt"
$d | Select-String -Pattern '^\|[^-]+' -allmatches

|column1|column2|cloumn3.............|last column
|Data1|Data2.........................|last data
|Data1|Data2.........................|last data
|column1|column2|cloumn3.............|last column
|Data1|Data2.........................|last data
|Data1|Data2.........................|last data
|column1|column2|cloumn3.............|last column
|Data1|Data2.........................|last data
|Data1|Data2.........................|last data
|*|
| |