0

i am aware of this doable by sed but only limited to one string let me explain let better, lets say in file 1 we got something like this

tom,123456789
steve,1478321
jon,315457

and in the second file file

tania,148321
john,123456789

so if a string in the second file matches the value in the first it will replace the value in the first file. Note i stink in programming i know this is prrettyy much doable via python or something but im clueless in them

3
  • Easy to do in awk. Read the second file into an array whose keys are the strings you want to match. Then when reading the first file, check if there's a matching array element and perform the replacement.
    – Barmar
    Commented Feb 10, 2017 at 22:57
  • Or, Barmar's method, but read the first file first, then the second in the same way (allowing the matching keys be replaced in the array) and then write the array at the end. Commented Feb 10, 2017 at 23:14
  • i almost have no experience in awk, how can it be done?
    – None111
    Commented Feb 10, 2017 at 23:34

1 Answer 1

1

Assuming you want to replace the first field if the second field matches, then you could use something like this:

awk -F, 'NR==FNR {a[$2]=$1; next} $2 in a {$1=a[$2]} 1' OFS=, file2 file1
john,123456789
steve,1478321
jon,315457
1
  • well it didn't work , its the five field on the first file and the first on the second file, the first file fields are separated by ","
    – None111
    Commented Feb 11, 2017 at 11:23

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.