【问题标题】:Adding data from different files and appending text - shell从不同文件添加数据并附加文本 - shell
【发布时间】:2014-06-02 15:45:48
【问题描述】:

我有 3 个文件

1) 测试1.txt

07777777
07777778
07777779

2) Test2.txt

A100192
A100193
A100194

3) Test3.txt

INST
DISC
INST

现在我想将这三个都合并到一个新文件中,如下所示

输出.txt

aaa<Test2.txt>  <Test1.txt><Test3.txt>bbb  <sometext>

有没有办法通过 shell script(bash) 来完成上述操作?

最终输出应该是这样的

"xyzA100192  07777777INSTxyz  xyz   " 

其中A100192 取自test2.txt07777777 取自test1.txtINST 取自test3.txt

同样,我需要填充 test1.txt、test2.txt 和 test3.txt 中的每个值,使其看起来像

xyzA100192  07777777INSTxyz  xyz   
xyzA100193  07777778DISCxyz  xyz   
xyzA100194  07777779INSTxyz  xyz   

非常感谢, V

【问题讨论】:

  • 你的输出格式不清楚。
  • 我不明白你的输出与他的输入是如何对应的。
  • 我不认为我理解您正在寻找的输出格式。您能给我们上面列出的三个文件的示例输出吗?
  • 最终输出应类似于“xyzA100192 07777777INSTxyz xyz”,其中 A100192 取自 test2.txt,07777777 取自 test1.txt,INST 取自 test3.txt
  • 您是否尝试过使用“pr”实用程序?它在哪些方面不符合您的需求(是的,您需要一些选择)

标签: bash shell


【解决方案1】:

这个可以的:

paste f1 f2 f3 | awk '{print "xyz"$1, $2$3"xyz", "xyz"}'

paste 单独并排打印所有三个文件:

$ paste f1 f2 f3
07777777        A100192 INST
07777778        A100193 DISC
07777779        A100194 INST

然后就是用awk 来欺骗一下。

$ paste f1 f2 f3 | awk '{print "xyz"$1, $2$3"xyz", "xyz"}'
xyz07777777 A100192INSTxyz xyz
xyz07777778 A100193DISCxyz xyz
xyz07777779 A100194INSTxyz xyz

请注意,您可以使用-v OFS 设置输出字段分隔符并定义例如选项卡。默认为空格:

$ paste f1 f2 f3 | awk -v OFS="\t" '{print "xyz"$1, $2$3"xyz", "xyz"}'
xyz07777777     A100192INSTxyz  xyz
xyz07777778     A100193DISCxyz  xyz
xyz07777779     A100194INSTxyz  xyz

【讨论】:

    【解决方案2】:

    仅基于您的评论

    啊,你确实编辑了它。

    exec 3< test1.txt 4< test2.txt 5< test3.txt
    test1open=true test2open=true test3open=true
    while $test1open && $test2open && $test3open ; do
        $test1opne && {
             read -u3 test1 || {
                 test1open=false
                 test1=
             }
        }
    
    [[etc.]]
    

    剩下的就是你的练习了。

    【讨论】:

      【解决方案3】:

      猛击:

      while read -ru 3 A && read -ru 4 B && read -ru 5 C; do
          echo "xyz${B}  ${A}${C}xyz  xyz   "
      done 3< Test1.txt 4< Test2.txt 5< Test5.txt
      

      【讨论】:

        猜你喜欢
        • 2012-03-16
        • 1970-01-01
        • 1970-01-01
        • 2015-07-14
        • 2019-05-10
        • 1970-01-01
        • 1970-01-01
        • 2011-07-31
        • 2015-03-28
        相关资源
        最近更新 更多