Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a log file that displays data 3 lines at a time. Like this:

1 data   
2 data   
3 data   
1 data   
2 data   
3 data   
1 data   
2 data   
3 data

I'd like to take each 3 lines and display them on 1 line like this:

1 data 2 data 3 data  
1 data 2 data 3 data  
1 data 2 data 3 data

I'd like to be able to cat this file and then pipe it through a command(s) that will do this for me. I suspect sed or awk are a solution.

share|improve this question
up vote 10 down vote accepted

You might be able to use paste:

$ paste - - - <data.txt
1 data      2 data      3 data   
1 data      2 data      3 data   
1 data      2 data      3 data
share|improve this answer
    
Paste is exactly what I was looking for, thank you. – Joe Mama Feb 26 at 20:46

Here's one way:

$ perl -pe 's/\n/ / unless $. % 3 == 0' file 
1 data    2 data    3 data   
1 data    2 data    3 data   
1 data    2 data    3 data
share|improve this answer
    
This works too, thanks. – Joe Mama Feb 26 at 21:09

Or also sed by "N"omming two lines and then nixing the newlines in the resulting buffer.

sed 'N;N;s/\n/ /g'
share|improve this answer
    
Nomming. Love it. – glenn jackman Feb 27 at 1:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.