Replace from refrence file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace from refrence file
# 1  
Old 12-14-2014
Replace from refrence file

Input file
Code:
 
  
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

Code:
 
 /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

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

Ouput I got
Code:
 
 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
Code:
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

Last edited by Scrutinizer; 12-14-2014 at 04:18 AM.. Reason: extra code tags
# 2  
Old 12-14-2014
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:
Code:
IFS="${IFS},"
while read base path key; do
  path=${path#/root_*/}
  path=${path%%/*}
  echo $base,$path,$key
done

which results in:
Code:
FS01,UKSR06_FS01,test1
FS04,UKSR04_FS04,test2
FS01,UKSR02_FS01,test4
FS01,UKSR03_FS01,test5
FS01,UKSR04_FS01,test6

This User Gave Thanks to derekludwig For This Post:
# 3  
Old 12-14-2014
Modification to your original attempt. Try:
Code:
awk 'NR==FNR{a[$1]=$2;next}{for (i in a) sub("[^,]*" i "[^,]*",a[i],$2)}1' FS=, OFS=, /tmp/reference /tmp/input

or

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

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Avoid overwriting backup file when multiple entries need to replace in one file input from another

Hello, I have been working on script in which search and replace the multiple pattern. 1. update_params.sh read the multiple pattern from input file ParamMapping.txt(old_entry|New_entry) and passing this values one by one to change_text.sh 2. change_text.sh read... (0 Replies)
Discussion started by: ketanraut
0 Replies

2. Shell Programming and Scripting

Perl script to read string from file#1 and find/replace in file#2

Hello Forum. I have a file called abc.sed with the following commands; s/1/one/g s/2/two/g ... I also have a second file called abc.dat and would like to substitute all occurrences of "1 with one", "2 with two", etc and create a new file called abc_new.dat sed -f abc.sed abc.dat >... (10 Replies)
Discussion started by: pchang
10 Replies

3. Shell Programming and Scripting

Refrence File replace (awk)

I know lot of very similar questions have been asked multiple time before ... this is a bit different Want to replace columns/subcolums from the reference file , if not present in the refrence file leave it as it is the complete column does not need to match the refernce file , values within... (2 Replies)
Discussion started by: greycells
2 Replies

4. Shell Programming and Scripting

Search and replace from file in awk using a 16 bit text file

Hello, Some time ago a helpful awk file was provided on the forum which I give below: NR==FNR{A=$0;next}{for(j in A){split(A,P,"=");for(i=1;i<=NF;i++){if($i==P){$i=P}}}}1 While it works beautifully on English and Latin characters i.e. within the ASCII range of 127, the moment a character beyond... (6 Replies)
Discussion started by: gimley
6 Replies

5. Shell Programming and Scripting

Replace from Refrence file

Need help with this .. Input file 234,6006016061302E006BE89E7FD64DE111,6006016061302E00B824FDA6D32CE111,Complete Refrence File 6006016061302E006BE89E7FD64DE111:vvv_222:433:RP_JRNL_LUN_3005 6006016061302E00B824FDA6D32CE111:vvv_444:344:laputa_LUN_95_L If $2 or $3 from input... (1 Reply)
Discussion started by: greycells
1 Replies

6. Shell Programming and Scripting

Optimised way for search & replace a value on one line in a very huge file (File Size is 24 GB).

Hi Experts, I had to edit (a particular value) in header line of a very huge file so for that i wanted to search & replace a particular value on a file which was of 24 GB in Size. I managed to do it but it took long time to complete. Can anyone please tell me how can we do it in a optimised... (7 Replies)
Discussion started by: manishkomar007
7 Replies

7. Shell Programming and Scripting

script to grep a pattern from file compare contents with another file and replace

Hi All, Need help on this I have 2 files one file file1 which has several entries as : define service{ hostgroup_name !host1,!host5,!host6,.* service_description check_nrpe } define service{ hostgroup_name !host2,!host4,!host6,.* service_description check_opt } another... (2 Replies)
Discussion started by: namitai
2 Replies

8. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

9. Shell Programming and Scripting

How to use refrence values inside sed command

I am using script S_db=`grep SRC_DB= ${login_txt} | cut -d= -f2` T_db=`grep TGT_DB= ${login_txt} | cut -d= -f2` today=`date +%Y%m%d` sed 's/USER_WORK/RAR_WORK_D1/g' < ${my_path}/files/${T_target}_${today}_DDL.txt > ${my_path}/files/temp9 But when I am trying to remove hardcode values... (1 Reply)
Discussion started by: scorp_rahul23
1 Replies

10. Shell Programming and Scripting

Refrence Shell variable in Perl code.

:confused: I have 2 files a & b Contents of a are:- -------------------------- This is a Good Boy This is a Good Boy This is a Very Good Boy This is a Good Boy This is a Good Boy --------------------------- The contents of b are ------------------- This is Testing Beta... (5 Replies)
Discussion started by: send2sridhar
5 Replies
Login or Register to Ask a Question