如何在一个制表符分隔文件中复制行并替换多个字符串
How to duplicate the lines and replace multiple string in ONE Tab Separated File
我有一个像 (in.txt) 这样的传入文件,它是一个制表符分隔文件 NO Header 行。
我想将每一行复制两次并进行替换(基于规则s)。
我是 *nux 的新手,我完全不知道什么工具可以帮助我做到这一点。
传入文件(in.txt。以制表符(\t)分隔)
A B C D E F G H
1 855211046 2/3/2015 0.00 4154245328852953 328573 1809 CC786875287728777
2 855211046 3/3/2015 0.10 5524415875875844 822409 1809 CC150330106885244
3 855211046 30/3/2015 5.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
非常感谢。
不难,但需要注意和打字:
awk '
/./{
split(,T,"/")
=sprintf("%d%02d%02d",T[3]-2000,T[2],T[1])
sub("\$","",);*=100
printf "^2.{9}.{7}.{16}(%d%0."12-length()"d).{8}(%d)\n",,0,
printf "^5(%d%0."18-length()"d).{7}(%-19d).{60}(%d)(%d).{10}\s{1}(%012d)\n",,0,,,,
...
}' in.txt >Outcome.txt
我相信你很容易根据上面的例子为第 3 行输出添加 printf
。
我有一个像 (in.txt) 这样的传入文件,它是一个制表符分隔文件 NO Header 行。 我想将每一行复制两次并进行替换(基于规则s)。 我是 *nux 的新手,我完全不知道什么工具可以帮助我做到这一点。
传入文件(in.txt。以制表符(\t)分隔)
A B C D E F G H
1 855211046 2/3/2015 0.00 4154245328852953 328573 1809 CC786875287728777
2 855211046 3/3/2015 0.10 5524415875875844 822409 1809 CC150330106885244
3 855211046 30/3/2015 5.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
非常感谢。
不难,但需要注意和打字:
awk '
/./{
split(,T,"/")
=sprintf("%d%02d%02d",T[3]-2000,T[2],T[1])
sub("\$","",);*=100
printf "^2.{9}.{7}.{16}(%d%0."12-length()"d).{8}(%d)\n",,0,
printf "^5(%d%0."18-length()"d).{7}(%-19d).{60}(%d)(%d).{10}\s{1}(%012d)\n",,0,,,,
...
}' in.txt >Outcome.txt
我相信你很容易根据上面的例子为第 3 行输出添加 printf
。