sof
sof

Reputation: 9649

How to use AWK to reformat lines?

How to use AWK to transform the lines of linear format to those of tabular format? e.g. to change the format of Ubuntu versions from:

13.10
Saucy
Salamander
14.04
Trusty
Tahr
14.10
Utopic
Unicorn

to:

13.10,Saucy,Salamander
14.04,Trusty,Tahr
14.10,Utopic,Unicorn

Upvotes: 1

Views: 124

Answers (2)

hek2mgl
hek2mgl

Reputation: 157957

I would use sed for that:

sed 'N;N;s/\n/,/g' input.txt

The command reads a line and appends two additional lines to the pattern buffer N;N. Then it replaces the newlines in between them by a , with s/\n/,/g .

Upvotes: 1

Ed Morton
Ed Morton

Reputation: 203324

$ awk '{ORS=(NR%3?",":"\n")}1' file
13.10,Saucy,Salamander
14.04,Trusty,Tahr
14.10,Utopic,Unicorn

Upvotes: 3

Related Questions