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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
04-26-2010, 07:58 AM
|
#1
|
|
Member
Registered: Dec 2005
Posts: 74
Rep:
|
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
|
|
|
|
04-26-2010, 08:12 AM
|
#2
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
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/'
|
|
|
|
04-28-2010, 02:49 AM
|
#3
|
|
Member
Registered: Dec 2005
Posts: 74
Original Poster
Rep:
|
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..
|
|
|
|
04-28-2010, 07:10 AM
|
#4
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:47 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|