shirop
shirop

Reputation: 107

How to Format ouput file using awk

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

Upvotes: 1

Views: 87

Answers (4)

accdias
accdias

Reputation: 5372

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.

Upvotes: 1

Ed Morton
Ed Morton

Reputation: 204731

$ 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

Upvotes: 1

Walter A
Walter A

Reputation: 20032

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

Upvotes: 0

James Brown
James Brown

Reputation: 37464

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

Upvotes: 0

Related Questions