Replace from refrence file

Input file

 
  
FS01,/root_vdm_9/UKSR06_FS01,test1
FS04,/root_vdm_11/UKSR04_FS04,test2
FS01,/root_vdm_7/UKSR02_FS01,test4
FS01,/root_vdm_12/UKSR03_FS01/dhdhdhdh,test5
FS01,/root_vdm_11/UKSR04_FS01/gggg,test6
 

Reference File

 
 /root_vdm_9/UKSR06_FS01,UKSR06_FS01
/root_vdm_11/UKSR04_FS04,UKSR04_FS04
 /root_vdm_11/UKSR04_FS01,UKSR04_FS01
/root_vdm_7/UKSR02_FS01,UKSR02_FS01
/root_vdm_12/UKSR03_FS01,UKSR03_FS01
 /UKSR03_FS01,UKSR03_FS01
 

I tried this

nawk -F"," 'NR==FNR{a[$1]=$2;next}{for (i in a) gsub(i,a,$0)}1' /tmp/reference /tmp/input

Ouput I got

 
 FS01,UKSR06_FS01,test1
FS04,UKSR04_FS04,test2
FS01,UKSR02_FS01,test4
FS01,UKSR03_FS01/dhdhdhdh,test5
FS01,/root_vdm_11/UKSR04_FS01/gggg,test6
 

Output I need

FS01,UKSR06_FS01,test1
FS04,UKSR04_FS04,test2
FS01,UKSR02_FS01,test4
FS01,UKSR03_FS01,test5
FS01,UKSR04_FS01,test6

Basically if a sub column or column from input file matches from $1 in the reference file , replace the complete column(not just the string ) of input file from $2 in the reference file ...

thx

From the looks of the results, would it be fair to say that you want the name of the first subdirectory in column two, that is if column two has /a/b/c/d, the result should be "b"? If so, a small shell (bash or ksh) script would work:

IFS="${IFS},"
while read base path key; do
  path=${path#/root_*/}
  path=${path%%/*}
  echo $base,$path,$key
done

which results in:

FS01,UKSR06_FS01,test1
FS04,UKSR04_FS04,test2
FS01,UKSR02_FS01,test4
FS01,UKSR03_FS01,test5
FS01,UKSR04_FS01,test6
1 Like

Modification to your original attempt. Try:

awk 'NR==FNR{a[$1]=$2;next}{for (i in a) sub("[^,]*" i "[^,]*",a,$2)}1' FS=, OFS=, /tmp/reference /tmp/input

or

awk 'NR==FNR{a[$1]=$2;next}{for (i in a) if (match($2,"[^,]*" i "[^,]*")) $2=a}1' FS=, OFS=, /tmp/reference /tmp/input
1 Like