1

How could I replace column 1 in File1 with column 2 in File2, when a match is found?

File1
id1 36987
id2 75685
id3 12345
id4 12896

File2
id1 ID1
id2 ID2
id3 ID3
id5 ID5

I'd like to write this on another File3, that should look like:

File3
ID1 36987
ID2 75685
ID3 12345
id4 12896

I'm currently (unsuccessfully) trying:

sed `File2.txt | awk '{print "-e s/"$1"/"$2"}'`<<< "File1.txt" 

2 Answers 2

2

With awk

$ awk 'NR==FNR {a[$1]=$2; next}; a[$1] {$1=a[$1]} 1' File2 File1
ID1 36987
ID2 75685
ID3 12345
id4 12896
2

I'd use sed to generate the sed script:

sed 's=^=s/=;s= =/=;s=$=/=' File2 | sed -f- File1

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.