LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-28-2006, 05:06 AM   #1
furquan
Member
 
Registered: Feb 2002
Posts: 30

Rep: Reputation: 15
SED - replace / insert


Hi every one

I am trying to write a script to change variables in a file and also insert additional info in the file.

I am able to do this via the command line using "sed" one by one. But when i try to put all the commands in script i get errors.

Can any one assit.

The code so far is :-

Quote:
#! /bin/bash
sed 's/ali/dubey/' -a sed -e '/user2=santosh/r append.txt' -a sed 's/192.168.9.89/120.78.56.34/' sms.conf
If i type these commands one by on on the command prompt they work fine.

This is for replace :-
Quote:
sed 's/ali/dubey/'sms.conf
This is for the Insert in a location :-
Quote:
sed -e '/user2=santosh/r append.txt' sms.conf
This is again replace :-
Quote:
sed 's/192.168.9.89/120.78.56.34/' sms.conf
but once i club it together in a script it does not work accordingly.

Any assistance ???

Thanks
 
Old 02-28-2006, 05:53 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,276

Rep: Reputation: 170Reputation: 170
do in a proper sed script maybe?

Code:
#!/usr/bin/sed -f

s/this/that/
s/THIS/THAT/g
etc etc...
 
Old 02-28-2006, 06:23 AM   #3
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
You need the '-e' option for every sed command including the first one. Also there isn't an '-a' option in my sed.

For long sed programs, it's best to create a sed script. Especially if you need to use branching, which you will if a pattern can stretch across two or more lines.
 
Old 02-28-2006, 06:28 AM   #4
furquan
Member
 
Registered: Feb 2002
Posts: 30

Original Poster
Rep: Reputation: 15
Thanks for the input biily,

but if you could explaing this part a bit more, would be greatful :-

Quote:
#!/usr/bin/sed -f

s/this/that/
s/THIS/THAT/g
Thanks
 
Old 02-28-2006, 06:32 AM   #5
furquan
Member
 
Registered: Feb 2002
Posts: 30

Original Poster
Rep: Reputation: 15
Hi jschiwal,

I used "-a" for "and". Could you tell me why do we need -e for every sed statement ?

Also could you tell me more about sed scripts.

Thanks
 
Old 03-01-2006, 06:58 PM   #6
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
fixed typo

I think that you are mixing in the syntax for the find command. The -e option allows you to enter more than one sed command. For example, suppose that you want to change every occurance of the word 'furquan' to 'Furquan' in a document, and also remove all blank lines.
sed -i -e 's/furquan/Furquan/g' -e '/^$/d' document

This is the same as having a sed scripts "changes.sed" with the contents:
s/furquan/Furquan/g
/^$/d

You would call this script like: sed -f changes.sed document

For some types of editing with sed, you have to use branches, and so you will need a sed script. Using a single
sed command with -e is best for simple changes.

I'll give you an example. Suppose that you use k3b to back up files in /home/username/Documents/ and you want to get rid of them. If you unzip the .k3b file you saved there will be a "mimetype" file and a "maindata.xml" file.
Examining the contents, you notice that the full pathname for each file is included in this document, but that the lines look like:
<url>/home/username/Documents/aDocument.pdf</url>
So you would like to A) remove all lines that don't match this pattern and B) remove the <url> and </url> part. So only 2 sed commands are necessary and you can put them in the same sed command and pipe the output to xargs.

sed -e '/^<url>/!d' -e 's/^<url>\(.*\)<\/url>/\1/' maindata.xml | tr '\n' '\000' | xargs -0 rm

The first command deletes all lines that don't start with '<url>'. The second command removes the <url> and </url> tags. The <url> and <\/url> parts in the search command serve as anchors, while the \(.*\) is a wild card that saves the contents in the middle. The \1 recalls the pattern found in the replace section of the sed substitute 's' command. The tr command replaces new-lines with null characters to make it easier to handle filenames that contain spaces.

The -e option is also used by the grep command. Suppose that you use the "locate" command but several screens are displayed. You can remove lines by piping the output through "grep":
locate pattern | grep -v -e pattern1 -e pattern2 -e pattern3

This way, you can press the up arrow, and keep adding patterns until you have the response pruned to how you want it.

Last edited by jschiwal; 03-04-2006 at 12:45 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed: insert a newline. Why does not it work? J_Szucs Linux - Software 4 03-19-2014 01:28 PM
Insert CRLF's with sed? unSpawn Programming 6 11-22-2006 07:46 AM
insert string with sed greg108 Programming 7 02-18-2005 01:11 PM
insert a symbol with sed tonton Programming 5 08-31-2004 11:33 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM


All times are GMT -5. The time now is 09:50 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration