Solved

Unix Shell script to edit a file.

Posted on 2013-01-10
8
521 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
 

Author Comment

by:07592161981m
ID: 38765946
The file is an xml file and the url line  start with <URL>http://...</URL>
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
If your app took Google’s lash recently, here are the 5 most likely reasons.
This video will demonstrate how to find the puppet warp tool from the edit menu and where to put the points to edit.
The viewer will learn how to dynamically set the form action using jQuery.

919 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

19 Experts available now in Live!

Get 1:1 Help Now