Joining 3 lines at a time

Hi,

I have a file which has the contents as below :

07:38:36 EST
date 20041117
07:39:06 EST
07:00:29 EDT
date 20050504
07:25:16 EDT
07:00:40 EDT
date 20050505
07:23:12 EDT

I need to delete the new line character from all lines except 3rd,6th,9th etc. lines so that i get the output as below :

07:38:36 EST date 20041117 07:39:06 EST
07:00:29 EDT date 20050504 07:25:16 EDT
07:00:40 EDT date 20050505 07:23:12 EDT

How can I achieve this ?? I tried some combinations of sed which I know but no success ...... for eg: sed 'N;s/\n/ /' can join pairs of lines ... but not 3 lines at a time .. I am searching for some kind of one liner like that .. not a script ..

Can anyone pls help ??

I think the below awk command will help you

awk 'NR % 3 !=0 {printf $0;printf " "} NR % 3 ==0 {print " "}' join_three_lines.dat

And a sed solution

[/tmp]$ echo "07:38:36 EST
date 20041117
07:39:06 EST
07:00:29 EDT
date 20050504
07:25:16 EDT
07:00:40 EDT
date 20050505
07:23:12 EDT
" | sed -e 'N;N;s/\n/ /g'
07:38:36 EST date 20041117 07:39:06 EST
07:00:29 EDT date 20050504 07:25:16 EDT
07:00:40 EDT date 20050505 07:23:12 EDT

Hi,

This gives the result like this :

07:38:36 EST date 20041117
07:00:29 EDT date 20050504
07:00:40 EDT date 20050505

Not showing the last time ....

I got it done through another way by appending a | to lines other than 3,6,9 etc and then using sed like this ...

cat filename | awk '(NR%3!="0") {print $0,"|"} (NR%3=="0") {print $0}' t1 | sed -e :a -e '/|$/N; s/|\n//; ta'

Not sure of any other better way to do this ...

That awk statment should be

 awk '($NR+1) % 3 !=0 {printf $0; printf " "} NR % 3 == 0 {print " "}'
[/tmp]$ echo "07:38:36 EST
date 20041117
07:39:06 EST
07:00:29 EDT
date 20050504
07:25:16 EDT
07:00:40 EDT
date 20050505
07:23:12 EDT
" | awk '($NR+1) % 3 !=0 {printf $0; printf " "} NR % 3 == 0 {print " "}'
07:38:36 EST date 20041117 07:39:06 EST  
07:00:29 EDT date 20050504 07:25:16 EDT  
07:00:40 EDT date 20050505 07:23:12 EDT  

Sorry. This will give you the expected result

awk '{printf $0;printf " "}NR % 3 ==0 {print " "}' join_three_lines.dat

Great Vino .. that worked fine ... One more doubt .... can u tell me whats the meaning of N in this sed command and how does that work here ?

From man sed

       n N    Read/append the next line of input into the pattern space.

See Oreilly's Sed and Awk - 6.1 Multiline Pattern Space

To join 3 lines try:
sed 'N;N;s/\n/ /'

4 lines? try:
sed 'N;N;N;s/\n/ /'

You would need the global flag at the end of those statements. Like,

sed 'N;N;s/\n/ /g'

paste -d"\t\t\n" <file_name>

Hi all ...

Thanks a lot for all replies ...

i got 4 solutions for to this ... and the sed one was simple .... I was new to n/N option in sed too .. thanks again to all ..

For future reference, the simplest way to join three lines is to use the paste command, like this...

paste -d ' ' - - - < file1

Wow !!! I was surprised to see such a small command doing this ... well cal u give an idea as to how it works ?? what do this 3 hyphens stand for ??

From man paste...