how to iterate though delimited values in bash

后端 未结 3 1973
攒了一身酷
攒了一身酷 2021-01-28 17:41

BASH

input: cat test

  1 a;b;c;d
  2 a;b;c;d
  3 a;b;c;d

desired output is:

  1 a
  1 b
  1 c
  1 d
  2 a
  2 b
  2 c
          


        
3条回答
  •  旧时难觅i
    2021-01-28 18:03

    Solution 1st: Could you please try following.

    awk -F'[ ;]' '{for(i=2;i<=NF;i++){print $1,$i}}' Input_file
    

    Solution 2nd: Adding another awk too now.

    awk '{gsub(/;/,ORS $1 OFS)} 1'  Input_file
    

提交回复
热议问题