【问题标题】:Linux shell on multiple files多个文件上的 Linux shell
【发布时间】:2014-03-26 05:44:04
【问题描述】:

我有一个文件 1,其中包含: aaaaaaa bbbbbbb ccccccc

文件 2: ddddddd eeeeeee fffffff

文件 n: ggggggg hhhhhhh iiiiiii

如何使用 regex shell linux 预添加thisistheresult 并生成如下结果文件: thisistheresultaaaaaaa,bbbbbbb,ccccccc thisistheresultddddddd,eeeeeee,fffffff thisistheresultggggggg,hhhhhhh,iiiiiii 感谢您的帮助!

【问题讨论】:

    标签: regex linux bash shell


    【解决方案1】:

    使用pastesed

    paste -s file* -d',' | sed 's/^/thisistheresult/'
    

    【讨论】:

    • 我得到了这个`thisistheresultaaaaaaaa
      ,bbbbbbb
      ,ccccccc
      `这是不对的。
    • 嗯,我得到的输出正是你想要的。我正在尝试了解您看到的行为。
    • @YOU,你能用paste -s file* -d,得到什么
    • 如何在评论中下线?
    • @YOU 这个命令不错。确保您的文件没有 Windows CR+LF。如果您从 Windows 导入文件,请针对 dos2unix 实用程序运行它。
    【解决方案2】:

    我假设文件名以file 开头。 以下代码应该可以在 bash 中运行。

    for i in file*; do echo -n 'thisistheresult'; paste -sd, $i; done
    

    【讨论】:

      【解决方案3】:

      试试这个。

      > for f in file*
      > do
      > echo thisistheresult$(tr '\n' ',' < $f)
      > done
      

      【讨论】:

        【解决方案4】:
        find -name 'file*' | xargs -iF tcsh -c "tr '\n', ',' < F | sed 's/^/thisistheresult/' && echo" > result.file
        

        【讨论】:

          【解决方案5】:

          试试这个:

          paste -s * | tr '\t' ',' | awk 'BEGIN { OFS = ""}{print "thisistheresult",$0}'
          

          输出:

          thisistheresultaaaaaaa,bbbbbbb,ccccccc
          thisistheresultddddddd,eeeeeee,fffffff
          

          如果你想在 "thisistheresult" 和 "aaaaaaa" 之间有空格,只需去掉 BEGIN 命令:

          paste -s * | tr '\t' ',' | awk '{print "thisistheresult",$0}'
          

          输出:

          thisistheresult aaaaaaa,bbbbbbb,ccccccc
          thisistheresult ddddddd,eeeeeee,fffffff
          

          【讨论】:

            猜你喜欢
            • 2014-03-27
            • 1970-01-01
            • 1970-01-01
            • 2013-05-06
            • 2018-06-09
            • 1970-01-01
            • 1970-01-01
            • 2015-03-31
            • 2015-04-17
            相关资源
            最近更新 更多