I have two files. The first file has 11 columns, for example:

1 2 3 4 5 6 7 8 9 10 11

The second has 10 columns and might look like this:

11 22 33 44 55 66 77 88 99 100

What I want to do is look at file1, and if column 7 is some value, for example say it's between 14 and 15, then replace column 9 of file1 with the the value of column 9 from file2. So in my example above, file1 would be rewritten as:

1 2 3 4 5 6 7 8 99 10 11

Checking to see if a column is between a certain value is trivial:

 awk '$7 < 15 && $7 >= 14

However, I'm having problems replacing column 9 of file1 with the value from the file2. file1 is NOT necessarily just one row. It could have any number of rows, and in every instance where the value is between 14 and 15 column 9 needs to be replaced. If the value is less than 14 or greater than 15 then the columns should remain as is. I don't believe this should be difficult, but I'm not having any luck. Help would be appreciated, and thanks in advance!

  • 1
    How many lines in file2? If many how they are correspond to each other: 1 to 1, 1 replacement to next … ?
    – Costas
    Sep 29, 2015 at 10:59
  • No, the files to not necessarily have a 1 to 1 correspondence.
    – Dani
    Sep 29, 2015 at 11:56
  • 1
    If file2 have several lines how it wil be choiced for exchange? Or value of 9'th column of 1st row is to used for all lines in file1?
    – Costas
    Sep 29, 2015 at 15:59

2 Answers 2


To use value of 9th field just 1st row of file2

awk 'NR==1{a=$9}FNR==NR{next}14<=$7&&$7<15{$9=a}1' file2 file1

To do line-by-line processing of two separate files in awk where both files have the same number of lines, and you only care about the corresponding line in the other file—that is to say, you want to look at line 1 of file1 and line 1 of file2, then look at line 2 of each, etc....

You are better off using paste file1 file2 | awk '{...}'

This is particularly true when each row of file1 has the same number of fields, and each row of file2 has the same number of fields (but they don't have to have the same number of fields as each other.)

If they have variable numbers of fields such that you need to determine which fields came from file1 and which fields came from file2, you can specify a delimiter to the paste command that you know does not occur in either file, and then split each line in awk based on that delimiter. For example:

paste -d: file1 file2 | awk -F: '{NF1=split($1,file1," "); NF2=split($2,file2," "); if (file1[7] <= 15 && file1[7] >= 14) { file1[9] = file2[9] }; for (i=1; i<=NF1; i++) {printf file1[i] " "}; printf "\n";}'

This does what you ask for, but it does more than that; it sets up your awk variables so that you have lines from both file1 and file2 available at once, along with NF1 and NF2 as the number of fields found in each file.

If I ever get around to learning the getline function in awk, I'll update this answer with "how to do it with getline()". :)

You must log in to answer this question.

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