LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-11-2010, 08:43 AM   #1
mariakumar
LQ Newbie
 
Registered: Oct 2010
Location: India
Distribution: Mac OS
Posts: 12

Rep: Reputation: 0
Replacing the text in same file using SED command


I have a String like "A.words=Ajay,Anil" in file A.And it contains a lot of other information also.
I wanted to replace "Ajay,Anil" with "Vijay,Vinay" with sed command with using existing file only(not using another file)

Can anyone help on it.
 
Old 10-11-2010, 08:47 AM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
If you understand sed syntax in the least little bit, this should be very easy. Syntax:

sed 's/OLD STRING/NEW STRING/'

That's the basic syntax. To operate on files in-place (i.e. not use a new file) you need the -i option to sed.

Please put together a sed command to do the job you want to do, and we shall comment on it and/or provide feedback if it does not work as expected.

P.S.:
Code:
man sed
 
Old 10-11-2010, 08:53 AM   #3
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 707

Rep: Reputation: 55
Code:
sed -i 's/Ajay,Anil/vijay,vinay/g' filename
 
Old 10-11-2010, 09:02 AM   #4
mariakumar
LQ Newbie
 
Registered: Oct 2010
Location: India
Distribution: Mac OS
Posts: 12

Original Poster
Rep: Reputation: 0
These values are coming dynamically in file(Ajay,Anil: vijay,vinay).

sed '/^A.words=$/s/=$/='$temp'/' $filename

what's wrong with the above script.Here temp contains (vinay,vijay)..
 
Old 10-11-2010, 09:09 AM   #5
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
I don't understand what you're doing there in post#4 - please observe the syntax shown by me and vinaytp.

However post#3 shows precisely the command you would need, to replace static instances of your string. If you want to use variables, I like to do it this way:
Code:
sed "s/$OLD_VAR/$NEWVAR/g" file
Note that I used "double" quotes around the expression. This allows the variables to be properly expanded, without messing around with a lot of escape characters. But keep in mind, if there are special characters inside your strings you're working with (the LHS and RHS in the sed expression) then you may still need more work. For simple alphanumeric and comma characters, you should have no troubles.
 
Old 10-11-2010, 09:10 AM   #6
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
First of all, you might need to change '$temp' to "$temp".
 
Old 10-11-2010, 09:13 AM   #7
mariakumar
LQ Newbie
 
Registered: Oct 2010
Location: India
Distribution: Mac OS
Posts: 12

Original Poster
Rep: Reputation: 0
First we need to find the string of "A.words" in file at starting the line, after equals(=) update with the new string.
 
Old 10-11-2010, 09:19 AM   #8
mariakumar
LQ Newbie
 
Registered: Oct 2010
Location: India
Distribution: Mac OS
Posts: 12

Original Poster
Rep: Reputation: 0
I have tried with double codes also.not working..
 
Old 10-11-2010, 10:18 AM   #9
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Quote:
Originally Posted by mariakumar View Post
First we need to find the string of "A.words" in file at starting the line, after equals(=) update with the new string.
Perhaps you mean something like:
Code:
sed -i "s/\(^A.words=\).*/\1$NEWSTRING/" file
This looks for the literal string "A.words=" at the start of the line, and replaces it with the same thing (that's why the \1 is there on the Right Hand Side), and the $NEWSTRING variable is put after the "=" sign.

EDIT: If you want to replace every occurrence, add the "g" to the end of the sed expression, inside the quotes - like I did in post #5
EDIT2: Never mind that edit - if the match is at the beginning of the line, then there can only be *one* match per line.

Is this what you're getting at?

Last edited by GrapefruiTgirl; 10-11-2010 at 10:26 AM. Reason: See EDIT
 
Old 10-12-2010, 12:20 AM   #10
mariakumar
LQ Newbie
 
Registered: Oct 2010
Location: India
Distribution: Mac OS
Posts: 12

Original Poster
Rep: Reputation: 0
Yes I got the solution same as above..
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] trying to insert text in the last line of a file with sed command.... Sayan Acharjee Linux - General 7 10-04-2010 05:00 AM
[SOLVED] Sed - Replacing only text with several specific lines excluded Potatos Linux - Newbie 6 06-17-2010 11:51 PM
Replacing text in files without using sed dfresh4130 Programming 16 05-28-2009 10:13 AM
Replacing text on specific lines with sed or awk? Lantzvillian Linux - Newbie 5 10-17-2007 09:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:46 PM.

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