Solved

Unix Shell script to edit a file.

Posted on 2013-01-10
8
522 Views
Last Modified: 2013-11-15
Hi Experts , I am looking for a script which i do this task repeatedly.

I am running a task that will create a file called diff_file. I want to back up this original diff_file in /tmp directory  and rename the diff_file to diff_change.
In the diff_change file I want to remove all the lines starting with <URL> and replace the strings :

tshost to host
tslat to lat
autodev to autoapp
100 to 200
gdev to mep
0
Comment
Question by:07592161981m
  • 4
  • 2
  • 2
8 Comments
 
LVL 31

Accepted Solution

by:
farzanj earned 500 total points
ID: 38765597
cp diff_file /tmp/diff_file.bak
mv diff_file diff_change
sed -i '/^<URL>/d;s/tshost/host/;s/tslat/lat/;s/autodev/autoapp/;s/100/200/;s/gdev/mep/' diff_change

Open in new window

0
 

Author Comment

by:07592161981m
ID: 38765787
Thank you for the response. If I Want to remove only the urls in the file , what will be the script.
0
 
LVL 31

Expert Comment

by:farzanj
ID: 38765921
Are your URLs starting with http?

you can do like

sed -i '/http:/d' filename

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:07592161981m
ID: 38765946
The file is an xml file and the url line  start with <URL>http://...</URL>
0
 
LVL 31

Expert Comment

by:farzanj
ID: 38765954
Oh, so you can simply use:

sed -i '/<URL>/d' filename

Open in new window

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 38766080
# farzanj's first suggestion corrected and improved:

cp diff_file /tmp/
sed -i '/^<URL>/d;s/tshost/host/g;s/tslat/lat/g;s/autodev/autoapp/g;s/100/200/g;s/gdev/mep/g' diff_file > diff_change

# note that this will change also:  lasthosts to lashost  and 1001 to 2001
# hope your want that
# > remove all the lines starting with
# we take that requirement verbatime, means no leading spaces allowed
0
 
LVL 31

Expert Comment

by:farzanj
ID: 38766680
@ahoffmann
My solution was correct and I had tested it.  Just putting globals (which was not mentioned by the asker) does not constitute correction.  You have essentially given my solution again

IMHO you need to correct you solution because when you use sed -i, you don't get any output and therefore your diff_change file will be empty.  Yes, you can remove -i and then it would work but IMHO, I am giving a point my point solution as was asked.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 38766757
@farzanj, you're right, it must be sed -e instead -i (c&p error)

just see that this question is in cygwin topic, I guess there need to be other solutions posted ;-)
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

If your app took Google’s lash recently, here are the 5 most likely reasons.
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
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 fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

803 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