LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-14-2011, 01:08 AM   #1
Arvind Shivaramakrishnan
LQ Newbie
 
Registered: Aug 2011
Posts: 12
Blog Entries: 2

Rep: Reputation: Disabled
How to edit and replace a particular entry using sed


Hi,
I am trying to write a script using sed to edit and replace the relay entry which is prefixed with "DS" in the sendmail.cf file, as show below.

[root@arvindlinux mail]# grep DS sendmail.cf
DS
# Return-Receipt-To: header implies DSN request
# DHParameters (only required if DSA/DH is used)
[root@arvindlinux mail]#

There are 3 entries with DS. But i want only the first one to be edited.

1st entry DS
2nd entry DSN
3rd DSA

I want only the first entry "DS" to be replaced with "DSabc"
i tried this using sed shown below but it is replacing all 3 entries starting with DS. Can someone please help?

[root@arvindlinux mail]# sed 's/DS/DSabc/g' sendmail.cf | grep DS
DSabc
# Return-Receipt-To: header implies DSabcN request
# DHParameters (only required if DSabcA/DH is used)
[root@arvindlinux mail]#

Thanks,
Arvind
 
Old 08-14-2011, 01:34 AM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Welcome to LQ,

try this one
Code:
sed 's/\<DS\>/DSabc/g'
 
1 members found this post helpful.
Old 08-14-2011, 01:42 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
There are several recommendations for sed tutorials here on LQ - search for some. There is also sed on sf.net; some nice doco there, including a page of one-liners. I would use an anchor for beginning of line
Code:
sed 's/^DS/DSabc/'
 
1 members found this post helpful.
Old 08-14-2011, 03:16 AM   #4
pix9
Member
 
Registered: Jan 2010
Location: Mumbai, India
Distribution: ArchLinux, Fedora 24, Centos 7.0
Posts: 177

Rep: Reputation: 19
Talking

man sed could help you a lot
 
Old 08-14-2011, 03:31 AM   #5
Arvind Shivaramakrishnan
LQ Newbie
 
Registered: Aug 2011
Posts: 12

Original Poster
Blog Entries: 2

Rep: Reputation: Disabled
Awesome.... Thanks a ton for the reply both of you. Both commands are working.
If i there are multiple occurances of the entry "DS " and i want to edit only a particular one, how do i do it?

i mean if there are 3 entries of "DS" how do i edit a particular one? for example say only the second occurance.
 
Old 08-14-2011, 03:51 AM   #6
pix9
Member
 
Registered: Jan 2010
Location: Mumbai, India
Distribution: ArchLinux, Fedora 24, Centos 7.0
Posts: 177

Rep: Reputation: 19
you can specify line number and it's ranges, but I would not recommend to do so, It can be a bad pragmatic in the case you are creating common script for files varying from various distribution.
still if you need syntax here it is

$sed '101,102 s/A/a/' /path/to/file
above command will change the capital "A" letter with small case "a" letter in between range of line no 101 to 105 ie( on line number 101, 102, 103, 104 and 105)
well in case you have occurrence of same word on same line in that case you will have to run sed twice without "g" option at the end

that is suppose if the word you want to replace comes on line number 9 and your line looks something similar to this
"hello my name is abc. abc means xyz."
and you want to change second instance of work "abc"
in that case you will have to run 3 sed on same line
like this

sed '30,30 s/abc/dummy/' /path/to/file
this will change like to some thing like this
"hello my name is dummy. abc means xyz." you need to preserve this output if your are directly saving this with "-i" option
now again run sed command on your temporary file
sed '30,30 s/abc/pqr/' /path/to/file
output will be some thing like this
"hello my name is dummy, pqr means xyz."
again you will have to save the output to some temporary file if you are not directly writing changes to file.
now again run sed command on this temporary file and change back dummy to abc again
like this
sed '30,30 s/dummy/abc/' /path/to/file
then finally you will get output like this
"hello my name is abc. pqr means xyz."

hope that was quite enough for you

thank you
pushkar.
 
1 members found this post helpful.
Old 08-14-2011, 06:53 AM   #7
Arvind Shivaramakrishnan
LQ Newbie
 
Registered: Aug 2011
Posts: 12

Original Poster
Blog Entries: 2

Rep: Reputation: Disabled
Wonderful Pushkar. Thanks again for the help.
 
Old 08-14-2011, 10:15 AM   #8
Arvind Shivaramakrishnan
LQ Newbie
 
Registered: Aug 2011
Posts: 12

Original Poster
Blog Entries: 2

Rep: Reputation: Disabled
Please let me know how to replace a path entry from a file using sed. Like for example below.

"/var/log" entry has to be changed to "/var/sulog" using sed. do we need to replace a escape character
 
Old 08-14-2011, 10:36 AM   #9
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by Arvind Shivaramakrishnan View Post
Please let me know how to replace a path entry from a file using sed. Like for example below.

"/var/log" entry has to be changed to "/var/sulog" using sed. do we need to replace a escape character
You can either escape the slashes or (better) use another separation character for sed's s-command, e.g.:
Code:
sed 's@/old/path@/new/path@'
Here the @ are separating the arguments for old and replacement pattern. Hence, you do not have to escape the slash.
 
1 members found this post helpful.
Old 08-14-2011, 12:00 PM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Instead of just asking us for help every time you want to do something, why not sit down and actually learn how to use sed? That's what I did.

Start with the first link here:

http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt

The other two pages are good places to look when you need to solve specific problems.

Also get on Google and find yourself a good Regular Expressions tutorial. Understanding regex is vital for fully unlocking the power of CLI tools like sed and grep.

Then when you have real problems you can come back for help, as opposed to just asking basic how-to questions.
 
1 members found this post helpful.
Old 08-14-2011, 02:14 PM   #11
Arvind Shivaramakrishnan
LQ Newbie
 
Registered: Aug 2011
Posts: 12

Original Poster
Blog Entries: 2

Rep: Reputation: Disabled
Thanks crts. But again when i had to replace a path with a @ it gave me a problem. like below. how do i resolve this?

had to replace "/var/log" with "user@servername.com"

so if i give sed 's@/var/log@user@servername.com@' this gives error...
how do i escape "@" used in "user@servername" ????
 
Old 08-14-2011, 02:35 PM   #12
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by Arvind Shivaramakrishnan View Post
Thanks crts. But again when i had to replace a path with a @ it gave me a problem. like below. how do i resolve this?

had to replace "/var/log" with "user@servername.com"

so if i give sed 's@/var/log@user@servername.com@' this gives error...
how do i escape "@" used in "user@servername" ????
You simply use a separator that does not appear in your patterns, e.g. if both '/' and '@' appear in your patterns then you could use '|' instead. You can use any character as separator you want:
Code:
s/pattern/replacement/
s@pattern@replacement@
s|pattern|replacement|
s,pattern,replacement,
... etc.

The other possibility is to escape the separator characters in your pattern like
Code:
s/\/path\/to\/file/\/path\/to\/replacement/
s@user\@domain.com@replamentuser\@domain.com
The backslash '\' is the escape character.

Last edited by crts; 08-14-2011 at 02:36 PM.
 
1 members found this post helpful.
Old 08-14-2011, 10:52 PM   #13
Arvind Shivaramakrishnan
LQ Newbie
 
Registered: Aug 2011
Posts: 12

Original Poster
Blog Entries: 2

Rep: Reputation: Disabled
Thanks
 
Old 08-27-2011, 07:16 PM   #14
Arvind Shivaramakrishnan
LQ Newbie
 
Registered: Aug 2011
Posts: 12

Original Poster
Blog Entries: 2

Rep: Reputation: Disabled
Hi All,
i have couple of queries
1. i am trying to change a complete line starting with a pattern using sed as below. i am replacing the entire line of file which has a string "ABCD"
to "EFGH" using below.

sed '
/ABCD/ c\
EFGH
' file_name

But when i had to replace "ABCD" with "EFGH'HIJK'KLMN" it did not work as below. I guess this is because of ' that i used before and after HIJK

sed '
/ABCD/ c\
EFGH'HIJK'KLMN
' file_name


This did not work. i guess i need to escape out '. can someone tell me how to do that or provide me a solution for this.

2. Second Query.
I am trying to insert a line into a file at a particular line number. Like iam trying to echo "This is Linux" at 45th line of a file which contains 100 lines and i did that using the commands below for Linux.

sed '45iThis is Linux" file_name
awk 'NR==45{print "newline"}1' file_name
Both the commands worked fine with Linux. But they are not working on a Solaris box. Can someone please provide me a solution for this.
 
Old 08-28-2011, 11:29 AM   #15
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by Arvind Shivaramakrishnan View Post
Hi All,
i have couple of queries
1. i am trying to change a complete line starting with a pattern using sed as below. i am replacing the entire line of file which has a string "ABCD"
to "EFGH" using below. But when i had to replace "ABCD" with "EFGH'HIJK'KLMN" it did not work as below. I guess this is because of ' that i used before and after HIJK This did not work. i guess i need to escape out '. can someone tell me how to do that or provide me a solution for this.
Yes, and you were given a solution in the posts above. Re-read them, and it will tell you how to 'escape' a character.
Quote:
2. Second Query. I am trying to insert a line into a file at a particular line number. Like iam trying to echo "This is Linux" at 45th line of a file which contains 100 lines and i did that using the commands below for Linux.

sed '45iThis is Linux" file_name
awk 'NR==45{print "newline"}1' file_name
Both the commands worked fine with Linux. But they are not working on a Solaris box. Can someone please provide me a solution for this.
You load the GNU version of sed onto the Solaris box. Since you don't say what version of Solaris, what version of sed is on it, or what errors you're getting, what can anyone tell you?

And again, instead of posting a new "please provide solution for xxxx with sed" question every time, why don't you take the time to LEARN SED?? There are many online tutorials, books, and guides to help you, instead of having others spoon-feed you each answer, because you don't want to learn.

Last edited by TB0ne; 08-28-2011 at 02:52 PM.
 
  


Reply



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 search replace tomerbd1 Linux - General 9 04-10-2008 04:31 AM
How do I replace ' with sed.... @ngelot Linux - Newbie 2 11-02-2007 07:04 PM
edit ldap entry philosophia Linux - Newbie 2 05-31-2007 08:07 PM
Need a mod to edit my HCL entry. qwijibow Linux - Hardware 1 01-11-2005 02:38 PM
edit HCL entry? psychobyte LQ Suggestions & Feedback 6 10-29-2004 07:32 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:43 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration