6

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.

10

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
  • Paste is exactly what I was looking for, thank you. – Joe Mama Feb 26 '16 at 20:46
2

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
2

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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