[SOLVED] How to edit and replace a particular entry using sed
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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]#
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
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.
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."
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.
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:
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.