-1

Can any one help me with this task to Compare columns of csv and txt file and update csv file with same entries that matches in both doc using bash script

file1.csv

tiger roar 12 7869 
dog bark 6 4455
cat mew 8 4432
puppy bark 2 4455

and so on...

file2.txt

7869 ability1
4455 abilityX
4455 ability2
4432 ability3
8899 ability4

and so on...

we need to match 4th column of csv file and 1st column of txt file and for same entries update 2nd column in csv file.

expected output

tiger roar 12 7869 ability1 
dog bark 6 4455 ability2 abilityX
cat mew 8 4432ability3
puppy bark 2 4455 ability2 abilityX

and so on...

if any one has any idea how we can make this possible in bash and python...

Jetchisel
  • 7,493
  • 2
  • 19
  • 18

1 Answers1

0

Using awk:

awk \
'if (FNR==NR) {
    ability[$1] = ability[$1]" "$2
}
else {
    print $0 ability[$4]
}' abilities.txt animals.csv > animals-updated.csv

Your “csv” file (comma separated variables) doesn’t actually contain commas (,). If it did, you could add FS="," directly above print.

dan
  • 4,846
  • 6
  • 15