I have 2 data files each containing one column. I want to make another data file by merging both the columns. I have the command line in shell but I don't know how it works.

Please explain elaborately the below command:

awk 'NR==FNR {a[i++]=$0};
     END     {for(j=0;j<i;) print a[j++],b[k++]}' \
  file1.txt file2.txt




I used the command above to write a shell script and got following output:

11 13
23 19
19 25
31 67
67 93

I want to know how this command line is working on this example to give the output?

  • There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs. – IconDaemon Mar 2 '17 at 13:06
  • 1
    Are you looking for a solution to accomplish your task (merging two single column files into a file with two columns), are you looking for an explanation on what the awk script does, or something else? Please be specific, and also provide samples of both the input files and the expected output (and/or maybe where the awk script is coming from). – nohillside Mar 2 '17 at 13:08
  • sorry for the inconvenience.i am learning shell script. I am just asking how my written command works ?i need an explanation how this command line – Pragyant Prada Behera Mar 2 '17 at 14:19
  • It's a rather "interesting" way to solve the problem. Did you get it from unix.com/shell-programming-and-scripting/…? Using paste file1.text file2.text would seem to be easier to use and understand :-) – nohillside Mar 2 '17 at 14:55

Well, part of learning to use Unix is to figure out what existing scripts are doing. In this case you need to know a bit about how awk works to understand the code. I will focus on describing the awk part, this should get you started in figuring out the rest.

Basically awk is a pattern-driven scripting language, where commands consist of both a (search) pattern/condition and a corresponding code block. During execution, any input files are read line by line and if the pattern/condition is true for a line, the code block is executed. There are special patterns BEGIN and END which are used to trigger code to get executed before the first line or after the last line is read.

In your example you have three pattern/code lines:

NR==FNR {a[i++]=$0};

NR and FNR are two special variables set by awk. You can look up their meaning with man awk to see that

NR     ordinal number of the current record
FNR    ordinal number of the current record in the current file

so basically this condition is true while lines from the first line are read (which means that a[i++]=$0 is executed once for each line from the first file) and false for all additional files. $0 is the current line of input.


This code block has no condition/pattern so it gets executed for every line read (from all files including the first one).

END     {for(j=0;j<i;) print a[j++],b[k++]}' 

This part runs after the last line of the last file has been read and processed.

With these basics you should be able to figure out the meaning of the different code blocks and variables yourself.

  • can you please define a and b? – Pragyant Prada Behera Mar 3 '17 at 15:36
  • a and b are user array variables (in awk variables don't need to be declared, they just get created by using them). If this is your first contact with awk I recommend reading one of the many introduction pages available. This will get you up to speed faster than anything I can explain here. – nohillside Mar 3 '17 at 16:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .