ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
I want to be able to find the lines that matches my input and change the N to a Y, but only for the lines that matches the name and not any other N's
My problem is the line does not always contain a P as it can be a D as well so my matching did not work.
If my script issues the name $1=triva the lines will change to:
Code:
trivia:P:Y
trivia:D:Y
I have the following code so far but as you can see it does not change the D's
Code:
sed -i 's/trivia:P:Y/trivia:P:N/g' servers.txt
Thanks
*** UPDATE ***
should I be using a method as follows? I am still stuck on the changing all instances though.
Code:
$1=server
sed -i 's/$server1:P:Y/$server:P:N/g' server.txt
sed -i 's/$server1:D:Y/$server:D:N/g' server.txt
Last edited by pobrika; 09-07-2009 at 08:18 PM.
Reason: Updated with new Idea?
#!/bin/bash
SERVER=$1
if [ -z $SERVER ]
then
echo "Usage: $0 servername"
exit
fi
sed -i "s/\(${SERVER}\:[PD]\:\)N/\1Y/" servers.txt
Notes:
Sed requires double quotes to interpret variables inside the regex
Colons need to be escaped
'$' will not be interpreted on the RHS so use backreference - '( )' / '\1'
Use '[]' to define a set or range to match
#!/bin/bash
SERVER=$1
if [ -z $SERVER ]
then
echo "Usage: $0 servername"
exit
fi
sed -i "s/\(${SERVER}\:[PD]\:\)N/\1Y/" servers.txt
Another way to do it, i.e. without having to concern yourself with the rest of the line is to do this:
Code:
sed -i "/$SERVER/s/:N$/:Y/" servers.txt
Two things to watch out for here. The first is the name of the server. If for some reason you're going to use a path, change all / to something else, e.g. @. The second thing is the -i option. This is very handy, however, you should skip it initially and preview using less to make sure you don't permanently ruin your file (or use something like -i.bak.)
Kevin Barry
Cheers everyone that is exactly what I was looking for I knew there must be a one liner for the sed command, annoyed with myself for not being able to find the answer on my own
I'll do some playing around and see which method works best in my script.
Just a follow up to say I managed to get my script to work, the only problems I had was the testing was done on my trusty rhel4 server and the script would not run on a solaris 8 platform.
Turned out the -i option did not work.
I ended up using:
Code:
sed "s/\(${SERVER}\:[PD]\:\)N/\1Y/" /usr/local/bin/servers.txt > /tmp/servers.txt
mv /tmp/servers.txt /usr/local/bin/servers.txt
I hate writing files out and back in as it is "messy" but it works.
Just a follow up to say I managed to get my script to work, the only problems I had was the testing was done on my trusty rhel4 server and the script would not run on a solaris 8 platform.
Turned out the -i option did not work.
I ended up using:
Code:
sed "s/\(${SERVER}\:[PD]\:\)N/\1Y/" /usr/local/bin/servers.txt > /tmp/servers.txt
mv /tmp/servers.txt /usr/local/bin/servers.txt
I hate writing files out and back in as it is "messy" but it works.
Not only that, if the file has multiple links (i.e. hard linked) they might not keep pointing to the same file unless you do this instead of mv:
Also, if this is in a script you might want to use mktemp in case it happens to get called twice concurrently. That way you won't overwrite something in progress.
Kevin Barry
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.