Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

transpose every n lines into a single line

I am trying to bring bunch of n lines into a single line,not sure how to do it. Any help is appreciated.

bunch of 11 words then a blank line and then again bunch of ll words and then blank like...so on.

Example:

cat filename

hi
hello
how
are
you
i
am
fine
how
are
you

hi
how
is
she
doing
i 
have
not 
herd 
from
her

..
..

desired output:

cat newFile

hi hello how are you i am fine how are you 
hi how is she doing i have not heard from her 
..
..

2 Answers

Through awk.

$ awk -v RS= '{gsub(/\n/, " ")}1' file
hi hello how are you i am fine how are you
hi how is she doing i  have not  herd  from her
like image 148
Avinash Raj Avatar answered Dec 06 '25 22:12

Avinash Raj


Treating blocks of text separated by blank lines as a single record is known as paragraph mode in awk:

$ awk -v RS= '{$1=$1}1' file
hi hello how are you i am fine how are you
hi how is she doing i have not herd from her
like image 45
Ed Morton Avatar answered Dec 06 '25 22:12

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!