Theodoros Emmanouilidis

Notes & Thoughts

awk Script That Adds Specified Prefix To Each Line

February10

This is a useful awk script that adds specified prefix to each line of a given text file.

1
awk -v PRE='THE_PREFIX_THAT_YOU_WANT' '{$0=PRE$0; print}' my_input_file.txt > my_output_file.txt

It’ s so simple with awk!

posted under Tip Of The Day | Comments Off on awk Script That Adds Specified Prefix To Each Line

awk Script To Merge Columns From Different Files

February7

This is a very helpful awk script to merge columns from different files into one single file.

Suppose we have two files with two columns each and the same number of lines.
Our goal is to merge column two from the first file with column one from the second.

First we merge the two files and then we use awk to select the desired columns and print them to a new file.

1
pr -m -t -s\  file1 file2 | awk '{print $2,$3}' >out_file.txt