可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have the below output file in the below format from a shell script
data text1
data1 text2
data3 text4,text5,text6,text7,text8,text9,text10,text12,text11,text13
data4 text53
data23 text45,text65,text98,text65`
Want to change the format of the output further like below to be more readable
data text1
data1 text2
data3 text4
data3 text5
data3 text6
data3 text7
data3 text8
data3 text9
data3 text10
data4 text53
data23 text45
data23 text65
data23 text98
data23 text65
please advise how to achieve the same using awk/sed ?
tried looking up for awk commands but not getting any clue as such , any help would be appreciated
Thanks
回答1:
$ awk -F'[ ,]+' '{for (i=2;i<=NF;i++) print $1, $i ORS}' file
data text1
data1 text2
data3 text4
data3 text5
data3 text6
data3 text7
data3 text8
data3 text9
data3 text10
data3 text12
data3 text11
data3 text13
data4 text53
data23 text45
data23 text65
data23 text98
data23 text65
回答2:
You can use something like this:
output | gawk '{split($2, a, /,/); for (i=1; i <= length(a); i++) {printf "%s %s\n\n", $1, a[i]}}'
Where output
is the output generated by your script.
Alternatively, you run it like this:
gawk '{split($2, a, /,/); for (i=1; i <= length(a); i++) {printf "%s %s\n\n", $1, a[i]}}' output_file
Edited to fix errors pointed by @Ed Morton and replaced awk
with gawk
since length(array)
is a GNU extension.
回答3:
Another using gsub
to ,
:
$ awk '{gsub(/,/, ORS ORS $1 OFS )}1' file
...
data3 text4
data3 text5
data3 text6
...
However, if $1
has, for example, &
in it, there will be trouble as it gets replaced with the match:
$ cat file
data& text4,text5,text6
$ awk '{gsub(/,/, ORS ORS $1 OFS )}1' file
data& text4
data, text5
data, text6
Fix that with another gsub
:
$ awk '{gsub(/,/, ORS ORS $1 OFS ); gsub(/,/,"\\&")}1' file
data& text4
data& text5
data& text6
回答4:
You can fold a line one time with
sed -r 's/([^ ]* +)([^,]*),(.*)/\1\2\n\n\1\3/g' file
Repeating it needs a marker a jump
sed -r ': a;s/([^ ]* +)([^,]*),(.*)/\1\2\n\n\1\3/g; t a' file