Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Unix Shell script to edit a file.

Posted on 2013-01-10
8
Medium Priority
?
528 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 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Technology Partners: 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!

Question has a verified solution.

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

If you're a modern-day technology professional, you may be wondering if certifications are really necessary. They are. Here's why.
Have you ever run into that annoying problem where the computer won't boot?  Wouldn't it be great if you had a tool that would make that disk boot again?  I have found one tool that works more often than not ...
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 …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

604 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