Solved

Unix Shell script to edit a file.

Posted on 2013-01-10
8
518 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
Comment Utility
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
Comment Utility
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
Comment Utility
Are your URLs starting with http?

you can do like

sed -i '/http:/d' filename

Open in new window

0
 

Author Comment

by:07592161981m
Comment Utility
The file is an xml file and the url line  start with <URL>http://...</URL>
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 31

Expert Comment

by:farzanj
Comment Utility
Oh, so you can simply use:

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

Open in new window

0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
# 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
Comment Utility
@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
Comment Utility
@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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Suggested Solutions

A high-level exploration of how our ever-increasing access to information has changed the way we do our jobs.
I use more than 1 computer in my office for various reasons. Multiple keyboards and mice take up more than just extra space, they make working a little more complicated. Using one mouse and keyboard for all of my computers makes life easier. This co…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now