Powershell / Batch将固定长度转换为逗号或管道分隔

时间:2014-03-22 16:16:44

标签: batch-file powershell

输入:

1116559   P1303251287   20130325225906CD   13013822   1   0000
1104220   P1303250282   20130325070119CD              1   0000
1064743   P1303251094   20130325191600CD              0   0000
1100819   P1303250369   20130325091722CD              0   0000
1101405   P1303250051   20130325010740CD              2   0000

我从我的尝试中获得了什么:

$lines = Get-Content "filenamehere.txt"
ForEach ($x in $lines) {
$y = "$($x[0..9] -join '')|$($x[10..23] -join '')|$($x[24..42] -join '')|
$($x[43..53] -    join '')|$($x[54..57] -join '')|$($x[58..61] -join '')|
$($x[126..138] -join '')"
$z = $y -join '|'
Write-Output $z | Out-File -FilePath "foo.txt" -Append}

我明白了:

1116559   |P1303251287   |20130325225906CD   |13013822   |1   |0000|
1104220   |P1303250282   |20130325070119CD   |           |1   |0000|
1064743   |P1303251094   |20130325191600CD   |           |0   |0000|
1100819   |P1303250369   |20130325091722CD   |           |0   |0000|
1101405   |P1303250051   |20130325010740CD   |           |2   |0000|

我不介意尾随空格,只要我可以进入这种格式。但是“Get-Content”将我的数据解析为数组并导入到SQL会给我一个错误。问题是,我该如何将其转换为CSV?

输出应为:

1116559|P1303251287|20130325225906CD|13013822|1|0000
1104220|P1303250282|20130325070119CD|        |1|0000
1064743|P1303251094|20130325191600CD|        |0|0000
1100819|P1303250369|20130325091722CD|        |0|0000
1101405|P1303250051|20130325010740CD|        |2|0000

6 个答案:

答案 0 :(得分:1)

我会用-replace

做到这一点
$Regex   = '(.{7})\s{3}(.{11})\s{3}(.{16})\s{3}(.{8})\s{3}(.{1})\s{3}(.{4})'
$Replace = '$1|$2|$3|$4|$5|$6'

(Get-Content "filenamehere.txt") -replace $Regex,$Replace |
 Set-Content "foo.txt"

答案 1 :(得分:0)

使用您的样本,您可以使用trim()

$lines = Get-Content "c:\temp\filenamehere.txt"
ForEach ($x in $lines)
 {
   $y = "$(($($x[0..9] -join '')).trim())|$(($($x[10..23] -join '')).trim())|$(($($x[24..42] -join '')).trim())|$(($($x[43..53] -join '')).trim())|$(($($x[54..57] -join '')).trim())|$(($($x[58..61] -join '')).trim())|$(($($x[126..138] -join '')).trim())"
   $z = $y -join '|'
   Write-Output $z | Out-File -FilePath "c:\temp\foo.txt" -Append
 }

也许它会删除太多空格。 我输出

1116559|P1303251287|20130325225906CD|13013822|1|0000|
1104220|P1303250282|20130325070119CD||1|0000|
1064743|P1303251094|20130325191600CD||0|0000|
1100819|P1303250369|20130325091722CD||0|0000|
1101405|P1303250051|20130325010740CD||2|0000|

从CSV的角度来看哪个应该更好。

答案 2 :(得分:0)

@echo off

for /F "tokens=1-6" %%a in (input.txt) do (
   if "%%f" neq "" (
      echo %%a^|%%b^|%%c^|%%d^|%%e^|%%f
   ) else (
      echo %%a^|%%b^|%%c^|        ^|%%d^|%%e
   )
)

输出:

C:\> test.bat
1116559|P1303251287|20130325225906CD|13013822|1|0000
1104220|P1303250282|20130325070119CD|        |1|0000
1064743|P1303251094|20130325191600CD|        |0|0000
1100819|P1303250369|20130325091722CD|        |0|0000
1101405|P1303250051|20130325010740CD|        |2|0000

答案 3 :(得分:0)

使用ConvertFrom-FixedLengths function即可:

Get-Content "C:\input.txt" | 
    ConvertFrom-FixedLengths 10,14,19,11,4,4 -Trim | 
    Foreach { @($_.Column1, $_.Column2, $_.Column3, $_.Column4.PadLeft(8, ' '), $_.Column5, $_.Column6) -Join "|" } |
    Out-File -FilePath "c:\output.txt"

或者,当然,如果你想用|字符作为分隔符创建一个csv文件,你可以这么做:

Get-Content "C:\input.txt" | 
    ConvertFrom-FixedLengths 10,14,19,11,4,4 -Trim | 
    Select Column1, Column2, Column3, @{ N = "Column4"; E = { $_.Column4.PadLeft(8) } }, Column5, Column6 | 
    Export-Csv -Path "C:\Output.csv" -NoTypeInformation -Delimiter "|"

或者,为了使其更简单,如果你想要一个csv文件并且不需要用空格填充第四列,你可以跳过最后一个样本中的“Select”行,使其成为:< / p>

Get-Content "C:\input.txt" | 
    ConvertFrom-FixedLengths 10,14,19,11,4,4 -Trim | 
    Export-Csv -Path "C:\Output.csv" -NoTypeInformation -Delimiter "|"

答案 4 :(得分:0)

对于体面的解决方案,您需要将内容作为固定长度字段处理,其他答案在此处执行。

如果你知道只有第4列可能是空白的,你可以通过用逗号替换11个字符的空格(在第4列有内容的行上不做任何事情)来为一次性脚本提供它,然后用逗号:

Get-Content "data.txt" | % { ($_ -replace "\s{11}", ",") -replace "\s+", "," } > out.txt

示例输出:

1116559,P1303251287,20130325225906CD,13013822,1,0000
1104220,P1303250282,20130325070119CD,,1,0000
1064743,P1303251094,20130325191600CD,,0,0000
1100819,P1303250369,20130325091722CD,,0,0000
1101405,P1303250051,20130325010740CD,,2,0000

答案 5 :(得分:0)

工作代码..

$('form').on('submit', function () {
    $('input[type=submit]', this).attr('disabled', 'disabled');
});