Tuesday 24 January 2017

Using AWK for column insertion within a text file

At times we may come across a requirement for inserting a column or maybe an entire file within another file. If the requirement is to merge the two files such that the columns of the second file commence right after the last column of the first file, then we can easily accomplish that using paste or join commands. But the task becomes tricky if the files are of different length columns and do not have a common column among them.
This article gives a quick demo about how we can use awk to add a new column/file to an existing file.

So, we have two files f1 & f2.

[root@alive ~]# cat f1
test1 testA
test2 testB
test3 testC
test4 testD
[root@alive ~]# cat f2
test7 testE
test8 testF
test9 testG
test10 testH


We want to merge file f2 with file f1 such that we would basically be adding two new columns to the file f1.

Here is the awk code to accomplish this task:

 awk '{getline new_col < "f2"} {print $0, new_col}' f1

The resulting output is as follows:

test1 testA test7 testE
test2 testB test8 testF
test3 testC test9 testG
test4 testD test10 testH


In the above example I used $0 with the awk print statement to print all columns of file f1 first followed by those belonging to file f2. We could've easily inserted the columns belonging to file f2 into some individual columns of file f1 by using individual column numbers instead of $0 in the print statement. Here's an example:

 awk '{getline new_col < "f2"} {print $1, new_col, $2}' f1

This awk statement will print the first column of file f1 first followed by the two columns comprised in file f2 and then the second column from file f1.

test1 test7 testE testA
test2 test8 testF testB
test3 test9 testG testC
test4 test10 testH testD

I hope this article was helpful and will definitely try to keep posting more tips & tricks like this in the future.

No comments:

Post a Comment

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...