用于映射列的awk命令&加入他们

时间:2017-05-07 14:16:02

标签: awk

cat file.txt

1; 2 | 56; 2

1 | 67

1 | 56

2; 1; 1; 1 | 56; 56; 56; 56

我在上面输入&我想把它表现为,直到管道和加入会像 管道左侧的第1列将与管道右侧的第1列连接, 所以预期的输出将是

1-56

2-2

这仅适用于第1行。对于所有行,应该以相同的方式发生。 你的帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

尝试:

awk -F'[;|]' '{for(i=1; i<=NF/2; i++) print $i "-" $(i+NF/2)}'  file

1-56
2-2
1-67
1-56
2-56
1-56
1-56
1-56

说明: 这种方法考虑到管道符号始终位于中间:

awk -F'[;|]' '               # Use the semicolon and the pipe symbol as field separator
  {
    for(i=1; i<=NF/2; i++)   # For every field on the left hand side of the pipe symbol
      print $i "-" $(i+NF/2) # Print each field on the left hand side (LHS) of the 
  }                          # pipe symbol together with its RHS counterpart 
'  file

答案 1 :(得分:0)

awk救援!

awk -F\| '{n=split($1,a,";");
           m=split($2,b,";"); 
           min=n<m?n:m; 
           for(i=1;i<=min;i++) print a[i]"-"b[i]}' file

1-56
2-2
1-67
1-56
2-56
1-56
1-56
1-56

也许你想在输出行之间放一个分隔符来表示记录......