我有一个传入的文件,如( in.txt ),它是一个制表符分隔文件,其中包含否标题行。 我希望每行重复两次并进行替换(基于规则)。 我是* nux的新手,我完全不知道哪些工具可以帮助我做到这一点。
传入文件( in.txt 。按标签分隔(\ t))
A B C D E F G H
1 855211046 2/3/2015 $170.00 4154245328852953 328573 1809 CC786875287728777
2 855211046 3/3/2015 $100.10 5524415875875844 822409 1809 CC150330106885244
3 855211046 30/3/2015 $105.00 4875875852875211 445092 1809 CC456387885245062
etc.
预期结果( Outcome.txt )
^2.{32}(855211046000).{8}(150302)
^5(855211046000).{7}(4154245328852953 ).{60}(150302)(328573).{10}\s{1}(000000017000)
^5(855211046000).{7}(4154245328852953 ).{60}(150302)(328573).{122}(000000017000)
^2.{32}(855211046000).{8}(150303)
^5(855211046000).{7}(5524415875875844 ).{60}(150303)(822409).{10}\s{1}(000000010010)
^5(855211046000).{7}(5524415875875844 ).{60}(150303)(822409).{122}(000000010010)
^2.{32}(855211046000).{8}(150330)
^5(855211046000).{7}(4875875852875211 ).{60}(150330)(445092).{10}\s{1}(000000010500)
^5(855211046000).{7}(4875875852875211 ).{60}(150330)(445092).{122}(000000010500)
规则
*1st record*
^2.{9}.{7}.{16}([Column B's data, 0 fill right till the position 12]).{8}([Column C's data but reformat to YYMMDD format])
^5([Column B's data, 0 fill right till the position 18]).{7}([Column E's data, SPACE fill right till the position 19]).{60}([Column C's data but reformat to YYMMDD format])([Column F's data]).{10}\s{1}([Column D's data but remove $ sign and then multiplied by 100])
^5([Column B's data, 0 fill right till the position 18]).{7}([Column E's data, SPACE fill right till the position 19]).{60}([Column C's data but reformat to YYMMDD format])([Column F's data]).{122}([Column D's data but remove $ sign and then multiplied by 100])
*2nd record*
same as 1st
*3rd record*
same as 1st
非常感谢。
答案 0 :(得分:0)
这并不难,但需要注意和打字:
awk '
/./{
split($3,T,"/")
$3=sprintf("%d%02d%02d",T[3]-2000,T[2],T[1])
sub("\\$","",$4);$4*=100
printf "^2.{9}.{7}.{16}(%d%0."12-length($2)"d).{8}(%d)\n",$2,0,$3
printf "^5(%d%0."18-length($2)"d).{7}(%-19d).{60}(%d)(%d).{10}\\s{1}(%012d)\n",$2,0,$5,$3,$6,$4
...
}' in.txt >Outcome.txt
我确信根据上面的示例,您可以轻松地为第3行输出添加printf
。