【问题标题】:How to duplicate the lines and replace multiple string in ONE Tab Separated File如何在一个制表符分隔文件中复制行并替换多个字符串
【发布时间】:2015-04-20 09:28:54
【问题描述】:

我有一个像 (in.txt) 这样的传入文件,它是一个制表符分隔文件和 NO 标题行。 我想将每一行复制两次并进行替换(基于规则s)。 我是 *nux 的新手,我完全不知道有什么工具可以帮助我做到这一点。

传入文件(in.txt。由 Tab(\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

非常感谢。

【问题讨论】:

  • 你试过什么?此外,当您在问题中标记它们时,您确实知道哪些工具可以帮助您...

标签: linux shell unix awk sed


【解决方案1】:

这并不难,但需要注意和一些打字:

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

我相信你很容易根据上面的例子为第三行输出添加printf

【讨论】:

    猜你喜欢
    • 2021-05-31
    • 1970-01-01
    • 2013-04-14
    • 1970-01-01
    • 2016-11-01
    • 2012-05-12
    • 2020-04-01
    • 2017-08-21
    • 1970-01-01
    相关资源
    最近更新 更多