how to iterate though delimited values in bash

后端 未结 3 1971
攒了一身酷
攒了一身酷 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条回答
  •  南笙
    南笙 (楼主)
    2021-01-28 18:20

    This might work for you (GNU sed):

    sed -r 's/^((\s*\S+\s*)[^;]*);/\1\n\2/;P;D' file
    

    Replace each ; by a newline followed by the key.

提交回复
热议问题