LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 04-26-2010, 07:58 AM   #1
alok.rhct
Member
 
Registered: Dec 2005
Posts: 74

Rep: Reputation: 16
Sed scripting "how to replace value for only last part of a pattern "


Hi,
I am new to sed and i am trying to make some script for remediation purpose using it.

All i want is to replace some part of pattern.
eg:
$1=" alok 10"

i want to change 10 with any number/string so that result comes as "alok 20". Is it possible with sed ?

I am looking for code some thing related as :
echo "alok 10" |sed -ie "s/$1/$1 10/g

I know the above is wrong , its just for your reference.

Also, while replying kindly explain wherever is required.
I am sucked, waiting for your reply..

--Alok
 
Old 04-26-2010, 08:12 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
I do not understand what you are trying to do.

In your example, you first set a variable equal to "alok 10". Your SED code is perfectly valid, and will give you: "alok 10 10"

Note: don't use the -i flag unless you are operating on a file and want to edit it "in place". You also do not need the -e flag.

How about:
Code:
echo "alok 10" | sed 's/10/20/'
 
Old 04-28-2010, 02:49 AM   #3
alok.rhct
Member
 
Registered: Dec 2005
Posts: 74

Original Poster
Rep: Reputation: 16
Hi,
sorry for that i was not clear enough in my last post!!

Actually I am not sure about the value of "alok ", it may be 10 or may be 100 , all i am trying to do is ,
i want to replace some parameter in system files , take an example of sshd.conf.
i wan to search parameter "PermitrootLoging Yes" (default setting in sshd.conf) and replace it with "PermitrootLogin No."

Now as per the above example given by you, there can be n number of "Yes" in that file. and I can't change all yes to no.

So i want a solution to replace pattern based on position with sed.

Hope now you can understand me..

Thanks for your time..
 
Old 04-28-2010, 07:10 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
I think this is what you are saying:
"if 'pattern' is found, then replace 'word' with 'newword' "

For this, you can use the SED address feature----Using the above definition, it would be:

sed '/pattern/s/word/newword/' filename > newfilename

Here is a really good tutorial on SED: http://www.grymoire.com/Unix/Sed.html
 
  


Reply

Tags
scripting, sed


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
sed - use sed to replace multiple instances from "x" to "y" in a line mrodmac Linux - General 4 02-02-2010 11:37 AM
?? sed: how to use "1 s/" with -i to replace pattern only once? GrapefruiTgirl Programming 27 01-01-2010 05:39 AM
replace a pattern with a line using sed/awk lokeshn05 Linux - Newbie 3 05-06-2009 03:01 PM
How can sed (or any other such utility) replace `x\"y' with `x\\\"y'? lumix Linux - Newbie 1 05-13-2008 08:05 PM
can I replace text with the result of "wc" using sed? BrianK Linux - General 1 04-21-2004 01:15 PM


All times are GMT -5. The time now is 11:24 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