Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unix how to concatenate lines based on pattern

Tags:

unix

awk

I want to join lines in a file as below.

Input

01EPH087362 SHHFHDH 3673
63737
Dhdhj
01EPH636363 DHHDH 
3637737
Hshshhd
01EPH7373838 HDJJDJ

Output

01EPH087362 SHHFHDH 3673 63737 Dhdhj
01EPH636363 DHHDH  3637737Hshshhd
01EPH7373838 HDJJDJ

I want the output as above; basically every line should start with 01EPH.

I have awk and sed but no luck. Please help if you know.

like image 689
suyog Avatar asked Jan 28 '26 04:01

suyog


1 Answers

$ awk '/^01EPH/{if (NR>1) print buf; buf=$0; next} {buf = buf OFS $0} END{print buf}' file
01EPH087362 SHHFHDH 3673 63737 Dhdhj
01EPH636363 DHHDH  3637737 Hshshhd
01EPH7373838 HDJJDJ
like image 176
Ed Morton Avatar answered Jan 31 '26 17:01

Ed Morton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!