bash sed/awk: replace string from a file searching in another file

1

3

i have a question about search and replacement on Debian. I have two files. One with:

a:b
c:d
e:f

and the other with:

 e
 c
 a`

In the second file I want to replace a with b, c with d, e with f. How can I do this?

maun goldman

Posted 2015-02-21T19:40:08.673

Reputation: 11

Answers

2

$ awk -F: 'FNR==NR{a[$1]=$2;next} {for (i in a)sub(i, a[i]);print}' file1 file2
 f
 d
 b

How it works

  • -F:

    This tells awk to split fields on the colon.

  • FNR==NR{a[$1]=$2;next}

    While reading the first file, this tells awk to create a dictionary a of the translations that we want to do.

  • for (i in a)sub(i, a[i])

    While reading the second file file, this tells awk to substitute in for every entry that we have stored in our dictionary a.

  • print

    After we have made the substitutions, this tells awk to print the line.

Replacing file2

To replace file2 with the new version:

awk -F: 'FNR==NR{a[$1]=$2;next} {for (i in a)sub(i, a[i]);print}' file1 file2 >tmp && mv tmp file2

With very recent versions of awk, there is a shortcut option for this: -i inplace. Beneath the surface, though, what this option does is exactly what the command above does.

John1024

Posted 2015-02-21T19:40:08.673

Reputation: 13 893

thanks a lots ! This works it print result on my console. but it doesn't replace in file2. how i do that – maun goldman – 2015-02-21T20:46:08.823

@maungoldman A method to replace file2 is included in the updated answer. – John1024 – 2015-02-21T20:54:57.767

1

A bit more complicated:

sed -f <(sed 's!\(.*\):\(.*\)!s/\1/\2/!' file1) file2
  • sed 's!\(.*\):\(.*\)!s/\1/\2/!' file1 reads the first file and outputs:

    s/a/b/
    s/c/d/
    s/e/f/
    
  • <(the_above) runs the above command with output to a temporary file.
  • sed -f <(…) file2 runs sed on file2 using that temporary file as an input (script) file.

To send the output back into file2, add a -i option:

sed -i -f <(sed 's!\(.*\):\(.*\)!s/\1/\2/!' file1) file2

G-Man Says 'Reinstate Monica'

Posted 2015-02-21T19:40:08.673

Reputation: 6 509