Solved

Search for a string in a line and replace it with another string from another file - Shell scripting

Posted on 2014-03-14
7
260 Views
Last Modified: 2014-03-17
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_serv1_data_03_12_2014_00_19/qa_ud1001/rtb-bed-serv1_ud1001
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_ud1002/rtb-bed-serv1_ud1002
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_ud1003/rtb-bed-serv1_ud1003
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_un1001/rtb-bed-serv1_un1001
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_un1002/rtb-bed-serv1_un1002

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_serv1_data_03_12_2014_00_19/qa_ud1001/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_serv1_data_03_12_2014_00_19/qa_ud1001/rtb-bed-serv1_ud2001"

and second line
and third line
and forth line so on ...

Can someone shed some light on it?

Thanks
Shankar
0
Comment
Question by:luser9999
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 23

Assisted Solution

by:savone
savone earned 250 total points
ID: 39929207
This may not be pretty....


[vonas@putor tmp]$ cat Dest_Lun.txt
ud2001
ud2002
ud2003
un2001
un2002


[vonas@putor tmp]$ cat CloneLuns.txt
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_ud1001/rtb-bed-serv1_ud1001
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_ud1002/rtb-bed-serv1_ud1002
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_ud1003/rtb-bed-serv1_ud1003
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_un1001/rtb-bed-serv1_un1001
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_un1002/rtb-bed-serv1_un1002


[vonas@putor tmp]$ for i in `cat CloneLuns.txt`; do OK=`echo $i | rev | cut -c 7- | rev`; for i in `cat Dest_Lun.txt`; do echo $OK$i;done;done
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_ud1001/rtb-bed-serv1_ud2001
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_ud1001/rtb-bed-serv1_ud2002
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_ud1001/rtb-bed-serv1_ud2003
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_ud1001/rtb-bed-serv1_un2001
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_ud1001/rtb-bed-serv1_un2002
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_ud1002/rtb-bed-serv1_ud2001
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_ud1002/rtb-bed-serv1_ud2002
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_ud1002/rtb-bed-serv1_ud2003
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_ud1002/rtb-bed-serv1_un2001
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_ud1002/rtb-bed-serv1_un2002
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_ud1003/rtb-bed-serv1_ud2001
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_ud1003/rtb-bed-serv1_ud2002
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_ud1003/rtb-bed-serv1_ud2003
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_ud1003/rtb-bed-serv1_un2001
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_ud1003/rtb-bed-serv1_un2002
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_un1001/rtb-bed-serv1_ud2001
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_un1001/rtb-bed-serv1_ud2002
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_un1001/rtb-bed-serv1_ud2003
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_un1001/rtb-bed-serv1_un2001
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_un1001/rtb-bed-serv1_un2002
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_un1002/rtb-bed-serv1_ud2001
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_un1002/rtb-bed-serv1_ud2002
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_un1002/rtb-bed-serv1_ud2003
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_un1002/rtb-bed-serv1_un2001
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_un1002/rtb-bed-serv1_un2002
[vonas@putor tmp]$
0
 

Author Comment

by:luser9999
ID: 39929387
Thanks savone,

We want the output of 5 lines only

/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_ud1001/rtb-bed-serv1_ud2001
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_ud1001/rtb-bed-serv1_ud2002
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_ud1001/rtb-bed-serv1_ud2003
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_ud1001/rtb-bed-serv1_un2001
/vol/clone_vol_rtb_bed_serv1_data_03_12_2014_00_19/qa_ud1001/rtb-bed-serv1_un2002


since Dest_Lun.txt  file has 5 entries.  Is there any modifications you do for 'for' loop.

thanks for helping
0
 
LVL 19

Accepted Solution

by:
simon3270 earned 250 total points
ID: 39929393
In a similar way but without the revs (and allowing for different length LUN names):
paste Source_Lun.txt Dest_Lun.txt |while read src dst; do
    sed -i "s/_${src}\$/_${dst}/" CloneLuns.txt
done

Open in new window

This does make some assumptions, such as no spaces in the source and dest names, exactly equal numbers of source and dest names, that none of the source names are subsets of each other (e.g. if you have ud1001 as a source, you don't also have aud1001) and that there is always an underscore before the LUN in CloneLuns.txt.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:luser9999
ID: 39929882
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
0
 
LVL 19

Expert Comment

by:simon3270
ID: 39930004
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:
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

Open in new window

(edited because I misspelt the ClonedLuns file name!)
0
 
LVL 23

Expert Comment

by:savone
ID: 39930481
@simon3270 - very elegant!  Nice work.
0
 

Author Comment

by:luser9999
ID: 39936123
@simon3270  - thanks :)
0

Featured Post

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you use Debian 6 Squeeze and you are tired of looking at the childish graphical GDM login screen that is used by default, here's an easy way to change it. If you've already tried to change it you've probably discovered that none of the old met…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

634 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question