2017-05-07 17 views
0

貓file.txt的awk命令用於映射列&加入他們

1; 2 | 56; 2

1 | 67

1 | 56

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

我上面輸入&我想它顯示爲,讀,直到管&加入會像在管道的左側 第一列會與上管的右側第一柱接合,所以 期望輸出將是

1-56

2-2

這是僅第一行。對於所有行都應該發生。 您的幫助將不勝感激。

回答

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 
+0

非常感謝。你能解釋一下這個命令嗎,它是怎麼寫的 – swapneil

+0

歡迎你。添加了一個解釋 – Scrutinizer

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 

也許你想放的輸出線之間的分隔符,表示記錄...