需要将固定宽度文件转换为在unix中分隔的“逗号”

问题描述 投票:3回答:3

需要将固定宽度文件转换为在unix中分隔的“逗号”。

k12582927001611USNA
k12582990001497INAS
k12583053001161LNEU

所需输出:

k,1258292700,1611,US,NA
k,1258299000,1497,IN,AS
k,1258305300,1161,LN,EU
unix
3个回答
5
投票

使用awksubstr()

awk -v OFS=, '{ print substr($0, 1, 1), substr($0, 2, 10), substr($0, 12, 4), substr($0, 16, 2), substr($0, 18, 2) }' file

输出:

k,1258292700,1611,US,NA
k,1258299000,1497,IN,AS
k,1258305300,1161,LN,EU

15
投票

像这样:

awk -v FIELDWIDTHS="1 10 4 2 2" -v OFS=, '{print $1,$2,$3,$4,$5}' file

OFS是输出字段分隔符,我将其设置为逗号。 FIELDWIDTHS变量为你带来了所有的魔力。

或者你可以在Perl这样做:

perl -ne 'm/(.)(.{10})(....)(..)(..)/; printf "%s,%s,%s,%s,%s\n",$1,$2,$3,$4,$5' file

或者,在sed像这样:

sed -E 's/(.)(.{10})(....)(..)(..)/\1,\2,\3,\4,\5/' file

1
投票

您可以通过以下方式管道文件:

awk '{print substr($0,1,1)","substr($0,2,10)","substr($0,12,4)","substr($0,16,2)","substr($0,18,2)}'

按照以下测试运行:

pax> echo 'k12582927001611USNA
k12582990001497INAS
k12583053001161LNEU' | awk '
{
    print substr($0,1,1)","substr($0,2,10)","substr($0,12,4)","
        substr($0,16,2)","substr($0,18,2)
}'

k,1258292700,1611,US,NA
k,1258299000,1497,IN,AS
k,1258305300,1161,LN,EU
© www.soinside.com 2019 - 2024. All rights reserved.