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

时间:2014-07-31 06:40:20

标签: unix

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

k12582927001611USNA
k12582990001497INAS
k12583053001161LNEU

必需的输出:

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

3 个答案:

答案 0 :(得分: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 :(得分: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

答案 2 :(得分: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