Bash 如何将破折号后的所有内容移动到新列?

Bash 如何将破折号后的所有内容移动到新列?,bash,awk,Bash,Awk,我有一个文件,它有一列文本,用一个破折号(-)分隔不同的行。我想把破折号下面的所有内容移到一个新的专栏。我的输入文件如下所示: jim bob - sally sue ed - bill - jerry curly phil - 我希望我的输出文件如下所示: jim sally bill jerry bob sue - curly - ed phil - -

我有一个文件,它有一列文本,用一个破折号(-)分隔不同的行。我想把破折号下面的所有内容移到一个新的专栏。我的输入文件如下所示:

jim
bob
-
sally 
sue 
ed
-
bill 
-
jerry
curly
phil
-
我希望我的输出文件如下所示:

jim    sally    bill    jerry
bob    sue      -       curly
-      ed               phil
       -                -

提前谢谢。

您可以对名为
input
的输入文件尝试以下操作:

 csplit -f tempfile input '/-/+1' '{*}'; paste tempfile*
使用
csplit
我们为所需输出中的每个“列”生成一个文件(
tempfile01
tempfile02
,…)。
接下来,我们合并这些临时文件。对于给定的示例输入,上述命令的输出为:

jim sally   bill    jerry   
bob sue     -       curly   
-   ed              phil    
    -               -   
添加
rm tempfile*
来进行必要的清理可能是个好主意

csplit -f tempfile input '/-/+1' '{*}'; paste tempfile* > output; rm tempfile*

我自己没有试过,所以它不太可能按你希望的方式工作。这是一个思考的机会,让它按照你想要的方式工作。在你“正确”后,请随意编辑这篇文章

开始{
列=1;
}
/^-$/ {
列++;
行=1;
下一个
}
{
字[列,行]=$1;
行[列]=++行;
}
结束{
for(col=1;col
有趣的练习,我的
gawk
主张:

gawk 'BEGIN{row=col=0}
      {d[col][row]=d[col][++row]=$1}
      $1=="-"{col++
              if (row>mrow){
                mrow=row
                }
              row=0
              next}
       END{
         for (r=0;r<=mrow;r++) {
           for (c=0;c<=col;c++) {

            printf("%s\t",d[c][r])
            }
            print ""
        }}' file

如果你有一个更大的真实数据集,你应该在你的问题中提到这一点。祝你好运。@F.克诺尔。对我来说是新的。我以为我什么都知道了:)
gawk 'BEGIN{row=col=0}
      {d[col][row]=d[col][++row]=$1}
      $1=="-"{col++
              if (row>mrow){
                mrow=row
                }
              row=0
              next}
       END{
         for (r=0;r<=mrow;r++) {
           for (c=0;c<=col;c++) {

            printf("%s\t",d[c][r])
            }
            print ""
        }}' file
jim    sally    bill    jerry       
bob    sue      -       curly       
-      ed               phil        
       -                -