Solved

Unix Shell script to edit a file.

Posted on 2013-01-10
8
525 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
[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
  • 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
Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

 

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

In this article, you will read about the trends across the human resources departments for the upcoming year. Some of them include improving employee experience, adopting new technologies, using HR software to its full extent, and integrating artifi…
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.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

724 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