luser9999
asked on
Search for a string in a line and replace it with another string from another file - Shell scripting
Hi Team,
I am writing a shell script which will procedure three output files
File: Source_Lun.txt
ud1001
ud1002
ud1003
un1001
un1002
File: Dest_Lun.txt
ud2001
ud2002
ud2003
un2001
un2002
File: CloneLuns.txt
/vol/clone_vol_rtb_bed_ser v1_data_03 _12_2014_0 0_19/qa_ud 1001/rtb-b ed-serv1_u d1001
/vol/clone_vol_rtb_bed_ser v1_data_03 _12_2014_0 0_19/qa_ud 1002/rtb-b ed-serv1_u d1002
/vol/clone_vol_rtb_bed_ser v1_data_03 _12_2014_0 0_19/qa_ud 1003/rtb-b ed-serv1_u d1003
/vol/clone_vol_rtb_bed_ser v1_data_03 _12_2014_0 0_19/qa_un 1001/rtb-b ed-serv1_u n1001
/vol/clone_vol_rtb_bed_ser v1_data_03 _12_2014_0 0_19/qa_un 1002/rtb-b ed-serv1_u n1002
Now, I want to produce the forth file which should replace the last string from file CloneLuns.txt with a string value from Dest_Lun.txt
for example:
Take a first line from the file CloneLuns.txt,
"/vol/clone_vol_rtb_bed_se rv1_data_0 3_12_2014_ 00_19/qa_u d1001/rtb- bed-serv1_ ud1001"
Take a first line from file Dest_Lun.txt,
ud2001
I want to replace only string ( ud1001) with ud2001 from both files and write it to another file ex: modified_luns.txt
so the output should be,
"/vol/clone_vol_rtb_bed_se rv1_data_0 3_12_2014_ 00_19/qa_u d1001/rtb- bed-serv1_ ud2001"
and second line
and third line
and forth line so on ...
Can someone shed some light on it?
Thanks
Shankar
I am writing a shell script which will procedure three output files
File: Source_Lun.txt
ud1001
ud1002
ud1003
un1001
un1002
File: Dest_Lun.txt
ud2001
ud2002
ud2003
un2001
un2002
File: CloneLuns.txt
/vol/clone_vol_rtb_bed_ser
/vol/clone_vol_rtb_bed_ser
/vol/clone_vol_rtb_bed_ser
/vol/clone_vol_rtb_bed_ser
/vol/clone_vol_rtb_bed_ser
Now, I want to produce the forth file which should replace the last string from file CloneLuns.txt with a string value from Dest_Lun.txt
for example:
Take a first line from the file CloneLuns.txt,
"/vol/clone_vol_rtb_bed_se
Take a first line from file Dest_Lun.txt,
ud2001
I want to replace only string ( ud1001) with ud2001 from both files and write it to another file ex: modified_luns.txt
so the output should be,
"/vol/clone_vol_rtb_bed_se
and second line
and third line
and forth line so on ...
Can someone shed some light on it?
Thanks
Shankar
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thanks simon3270. That worked like a charm.
One final question - how to redirect the output to a file so that CloneLuns.txt contains the original data and modified data will be redirected to Modified-Luns.txt file.
i tried
paste Source_Lun.txt Dest_Lun.txt |while read src dst; do
sed -i "s/_${src}\$/_${dst}/" CloneLuns.txt > Modified-Luns.txt file
done
but does not seem to be working. I even tried below,
paste Source_Lun.txt Dest_Lun.txt |while read src dst; do sed -i.bak "s/_${src}\$/_${dst}/" CloneLuns.txt ; done
both original and .bak files have same data (which is modified/replaced data)
Thanks for your time on this
One final question - how to redirect the output to a file so that CloneLuns.txt contains the original data and modified data will be redirected to Modified-Luns.txt file.
i tried
paste Source_Lun.txt Dest_Lun.txt |while read src dst; do
sed -i "s/_${src}\$/_${dst}/" CloneLuns.txt > Modified-Luns.txt file
done
but does not seem to be working. I even tried below,
paste Source_Lun.txt Dest_Lun.txt |while read src dst; do sed -i.bak "s/_${src}\$/_${dst}/" CloneLuns.txt ; done
both original and .bak files have same data (which is modified/replaced data)
Thanks for your time on this
That doesn't work because of the "-i" on the sed command (which sort of edits "in place", so that the original file is overwritten).
The problem is that the file is modified by one LUN at a time, so you need to apply the changes to one file repeatedly (the first edit replaces ud1002 with ud2001, the second replaces ud1002 with ud2002 and so on). That's also why the .bak version failed - By the time it had done the last replacement (un1002 -> un2002), the .bak file had been overwritten four times with slowly changing replacements.
Easiest would be to copy CloneLuns.txt to Modified-Luns.txt before the loop, then work on Modified-Luns.txt instead:
The problem is that the file is modified by one LUN at a time, so you need to apply the changes to one file repeatedly (the first edit replaces ud1002 with ud2001, the second replaces ud1002 with ud2002 and so on). That's also why the .bak version failed - By the time it had done the last replacement (un1002 -> un2002), the .bak file had been overwritten four times with slowly changing replacements.
Easiest would be to copy CloneLuns.txt to Modified-Luns.txt before the loop, then work on Modified-Luns.txt instead:
cp CloneLuns.txt Modified-Luns.txt
paste Source_Lun.txt Dest_Luns.txt |while read src dst; do
sed -i "s/_${src}\$/_${dst}/" Modified-Luns.txt
done
(edited because I misspelt the ClonedLuns file name!)
@simon3270 - very elegant! Nice work.
ASKER
@simon3270 - thanks :)
ASKER
We want the output of 5 lines only
/vol/clone_vol_rtb_bed_ser
/vol/clone_vol_rtb_bed_ser
/vol/clone_vol_rtb_bed_ser
/vol/clone_vol_rtb_bed_ser
/vol/clone_vol_rtb_bed_ser
since Dest_Lun.txt file has 5 entries. Is there any modifications you do for 'for' loop.
thanks for helping