UPDATE: I have another article on this topic called PASTE primer. Which covers the simplest way to join any number of lines without AWK or SED which is much easier.

…Joining Lines…
Imagine a file with the following text, this will be our test subject.

Reading the test.txt with cat.

Has the output of this

How do we join every other line? (this is 2 lines joined)

3 ways (Siting: http://stackoverflow.com/questions/8987257/concatenating-every-other-line-with-the-next)

With SED:

Results of the above cat to sed pipe


Result with paste and cat:

 With AWK:

Here are the results:

NOTE: the 2 different ways to excute
1. cat the file and pipe to awk or read the file with awk
2. have printf be used with () or without parenthesis, either way all achieve the same result

What happens above with the awk script is pretty simple.
Awk executes the command/commands in {} for every line
The 2 commands are
getline b; printf("%s %s\n",$0,b); First lets look at print line
It takes 2 strings variables (which are the %s ) and prints them side by side followed by a newline char ( \n )
The first string is $0 , which means current string. The following string on the same line and just a space away is the b variable. But whats the b variable?
Looking at the previous line “ getline b ” we see that b is used. Getline gets the next line for awk and stores it in the given variable. The best part is that on the next run of awk, the current string will not be the one that was just read by getline, but one ahead
So if we break it down

 3 lines joined?


Note just add a getline c (had to use a different variable for c). Recall that getline moves the currentline one step forward. getline always gets the next line

so that


NOTE: the above 4 different ways to run using the 2 different ways to execute still apply. Im just showing one variation of it, because it gets the point across. This goes true for the next example as well

4 lines joined?


N lines joined?

You get the pattern. I hope…

Just go ahead add a
getline X; and dont forget to add a " %s"  to the printf along with the  ,X  outside of the double quotes so that the %s can get filled with the value of X. Where X is a different variable thats not been used

The end..

Leave a Reply

Your email address will not be published. Required fields are marked *