Viru
Viru

Reputation: 175

merge specific line using awk and sed

I want to merge specific line

Input :

AAA
BBB
CCC
DDD
EEE
AAA
BBB
DDD
CCC
EEE

Output Should be

AAA
BBB
CCC DDD
EEE
AAA
BBB
DDD
CCC EEE

I want to search CCC and merge next line with it.

I have tried with awk command but didn't get success

Upvotes: 1

Views: 103

Answers (2)

Alper
Alper

Reputation: 13220

Use awk patterns, if the line matches /CCC/ then print the line with a space at the end and go on to the next line. Otherwise (1), print the line.

awk '/CCC/ { printf("%s ", $0); next } 1' file

Upvotes: 2

Steve
Steve

Reputation: 54392

Using :

sed '/CCC/ { N; s/\n/ / }' file

Using :

awk '{ ORS=(/CCC/ ? FS : RS) }1' file

Upvotes: 1

Related Questions