Sample Header Ad - 728x90

find and replace with the value in another file

1 vote
3 answers
2435 views
I have two files with different formats with columns tab spaced. I have to compare the columns column1, column2 of file1 with file2. If they matches, I need to replace the value in column6 of file1 with the value in column3 of file2. I have tried using awk but I am not able to replace the value. Could you please advise on the below snippet ? awk 'FILENAME == ARGV { m[$1,$2] = $6; next; } { if (($1,$2) in m) { m[$6]= $3; print m[$6]; } }' file1 file2 top few lines of file1 1201 12011 1 0 0 0 1 1202 12021 1 0 0 0 1 1203 12031 1 0 0 0 1 1204 12041 1 0 0 0 2 1207 12071 1 0 0 0 2 1209 12091 1 0 0 0 1 1210 12101 1 0 0 0 1 1212 12121 1 0 0 0 1 1213 12131 1 0 0 0 1 1214 12141 1 0 0 0 2 top few lines of file2 1201 12011 1 1202 12021 1 1203 12031 1 1204 12041 1 1206 NA 1 1207 12071 2 1208 NA 1 1209 12091 2 1210 12101 2 I want to assign the values from file2 to file1 column as I would like to write the updated content into another file out.txt Tried the below code as per the comments: awk '{ if (FNR==NR) { a[FNR]=$1;b[FNR]=$2;c[FNR]=$3} else { if (a[FNR] == $1 && b[FNR] ==$2) { $6=c[FNR]} else {$6=$6}; print $0; } }' file2 file1 Got this output 1201 12011 1 0 0 1 1 1202 12021 1 0 0 1 1 1203 12031 1 0 0 1 1 1204 12041 1 0 0 1 2 1207 12071 1 0 0 0 2 1209 12091 1 0 0 0 1 1210 12101 1 0 0 0 1 1212 12121 1 0 0 0 1 1213 12131 1 0 0 0 1 1214 12141 1 0 0 0 2
Asked by Prradep (203 rep)
Aug 4, 2015, 01:29 PM
Last activity: Jul 15, 2025, 09:02 PM