I am not an experienced Linux user. I am trying to select column data from 2 separate files and write to a third file using awk. I have tried to paste the files together i.e. paste file1 file2 and then awk but the data is appended on the next line (alternating).

The data looks like this:


HZ880   0.00 

HAM86   1.13 

HAM40   1.60 


HZ880  -31.816826 115.757963   35.8909  0.0170  -.0170

HAM86  -31.824923 115.761507   33.6108  0.0165  -.0165

HAM40  -31.828528 115.762380   38.8434  0.0163  -.0163

How do I create a new file with column2 (file1) and column4 (file2)?

I have tried the following:

paste ${LEV_IN1} ${LEV_IN2} | awk '{print $2,$4}' > ${TEMP2}

where LEV_IN1 is file1 and LEV_IN2 is file2

What am I doing wrong?

  • 1
    Show your desired output. – John1024 Aug 10 '15 at 5:36

When you give file names one after another first it count all the columns from file1 and then start counting from next number to the file2. That means it counts 2 columns of file1 and start column 3,4,5.... and so on of the file2.

To get the column 4 of the file2 you need to write $6 because command start counting from 3 in file2.

This will give you the desire output (change $6 with other number if you mean to print anyother column from file2):

paste file1 file2 | awk '{print $2 " " $6}' > outputfile.txt

Another way (Here $4 means column 4 of the file2):

paste <(awk '{print $2}' file1) <(awk '{print $4}' file2) > outputfile.txt


0.00    35.8909

1.13    33.6108

1.60    38.8434

Following is the pure awk solution:

awk 'NR==FNR{a[NR]=$2; next} {print a[FNR], $4}' file1 file2 > outputfile.txt
  • Thank you for your responses. When I try this, the data is correct until line 10 when the columns start being written to separate lines – Robert Holloway Aug 10 '15 at 6:41
  • Pure awk is awk '{printf "%s ", $2;getline <"file2";print $4}' file1 – Costas Aug 10 '15 at 7:18

There is no need to use awk for the task

join --nocheck-order -o 1.2,2.4 file1 file2


paste file1 file2 | tr -s ' ' '\t' | cut -f2,6

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.