需要将固定宽度文件转换为在unix中分隔的“逗号”。
k12582927001611USNA
k12582990001497INAS
k12583053001161LNEU
所需输出:
k,1258292700,1611,US,NA
k,1258299000,1497,IN,AS
k,1258305300,1161,LN,EU
使用awk
和substr()
:
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
像这样:
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
您可以通过以下方式管道文件:
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