0

I need to use one file as an input to find and replace in another file. The following command does so, but only for the first column for each line. I need this to happen for every column.

awk -F',' 'NR==FNR{a[$1]=$2} NR>FNR{$1=a[$1];print}' OFS=',' file1 file2 > fileout

The two files look like this

file1 (the key):

0,name0
1,name1
2,name2
3,name3
4,name4

file2:

23,45,0
41,1,2,4
4,5,22,100
10,20,31,51,1
33,3
16,111,3

The output should be:

23,45,name0
41,name1,name2,name4
name4,5,22,100
10,20,31,51,name1
33,name3
16,111,name3
  • Why not apply one column at a time? – RSFalcon7 Aug 21 '18 at 20:09
  • had a typo in my previous loop. got it to work awk -F',' 'NR==FNR{a[$1]=$2} NR>FNR{for (i=1;i<=NF;i++) $i=a[$i];print}' OFS=',' – Johnny Aug 21 '18 at 20:32
1

This worked:

awk -F',' 'NR==FNR{a[$1]=$2} NR>FNR{for (i=1;i<=NF;i++) $i=a[$i];print}' OFS=',' file1 file2 > fileout

Thanks

1

I don't think above will work. Result on my system:

,,name0
,name1,name2,name4
name4,,,
,,,,name1
,name3
,,name3

Try instead

awk -F',' 'NR==FNR{a[$1]=$2; next} {for (i=1;i<=NF;i++) if (a[$i]) $i=a[$i]} 1' OFS=',' file1 file2
23,45,name0
41,name1,name2,name4
name4,5,22,100
10,20,31,51,name1
33,name3
16,111,name3
1

It could also be done with sed:

sed -f <(sed 's|,|$/,|;s|^|s/,|;s|$|/|' f1) f2

As pointed out by Kusalananda, this solution uses process substitution, which not all common shells support. However, there are other ways to achieve the same effect.

How does this work? The basic idea is to transform file f1 into a sed program that will in turn transform file f2. The sed command in parentheses transforms each line of file f1 into an s/// command.

  • Since the given command is fairly complex, it would be good to have an explanation of how it operates. Also, it seems to require bash (for the process substitution), and I'm not quite sure why you escape that $ in the first substitution's replacement part (but maybe you could explain that too?). – Kusalananda Aug 22 '18 at 10:08
  • The escape was unnecessary. Thanks for pointing that out. I think the program is a little clearer now. – Michael Vehrs Aug 22 '18 at 10:26

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.