perl - Conditional string replacement based on matched column -
i'd match string present in columnx , replace fixed string in columny. example how match strings based on column3 in file1 file2 in below example , selectively replace column2 of file1 fixed string "au" whenever match found. if no match found, rows in file1 should printed output. both file1 & file2 contains more 100k such lines.
file1:
0,ds,"c_3363/y" 1,ds,"c_3363/y" 0,uu,"c_3364/y" 1,uu,"c_3364/y"
file2
0, "c_3364/y" 1, "c_3364/y"
desired output:
0,ds,"c_3363/y" 1,ds,"c_3363/y" 0,au,"c_3364/y" 1,au,"c_3364/y"
another gnu awk solution single fs:
$ cat tst.awk begin {fs=","} nr==fnr && sub(/ /, "", $2) {a[$2]++; next} ($3 in a){ printf "%s,au,%s\n", $1,$3; next}1
same, commandline:
awk -f, 'nr==fnr && sub(/[[:space:]]/,"",$2){a[$2]++; next} ($3 in a){ printf "%s,au,%s\n", $1,$3; next}1' input2.txt input1.txt
with input files:
$ cat input2.txt 0, "c_3364/y" 1, "c_3364/y" 1, "a_3364/y" 1, "b_3364/y" $ cat input1.txt 0,ds,"c_3363/y" 1,ds,"c_3363/y" 0,uu,"c_3364/y" 1,uu,"c_3364/y"
you'll get:
$ awk -f, 'nr==fnr && sub(/[[:space:]]/,"",$2){a[$2]++; next} ($3 in a){ printf "%s,au,%s\n", $1,$3; next}1' input2.txt input1.txt 0,ds,"c_3363/y" 1,ds,"c_3363/y" 0,au,"c_3364/y" 1,au,"c_3364/y"
Comments
Post a Comment