Using pssh to replace text in place - sed

Anyone here used pssh to modify files in place, I've tried attempts like so;
while read line;
do
pssh --inline-stdout -H "$line" "hostname;
sudo sed -i \'s/search domain123.local/search domain123.local domain456.local/g\' /etc/resolv.conf;
sleep 1";
done < <(cat listOfIPs.txt)
Although pssh returns Success status, it doesn't amend resolv.conf on the actual host - am I missing something obvious in the pssh or sed command?
Thanks!

I found by altering the quotes, from single to double in the sed part, it seemed to fix it:
while read line;
do
pssh --inline-stdout -H "$line" "hostname;
sudo sed -i \"s/search domain123.local/search domain123.local domain456.local/\" /etc/resolv.conf;";
done < <(cat listOfIPs.txt)
And I also removed the /g but I don't think that really made a diff

Related

SED inplace file change inside make - How?

sed inplace change on a file is not working inside Make object.
I want to replace a line in a file with sed called in a make object. But it does not seem to be working. How can I fix this?
change_generics:
ifeq ($(run_TESTNAME), diagnostics)
ifeq ($(run_TESTCASE), 1)
sed -i -e "s/SIM_MULTI\==[a-z,A-Z]*/SIM_MULTI=TRUE/" ./generics.f
else ifeq ($(TESTCASE), 2)
sed -i -e "s/SIM_MISSED\==[a-z,A-Z]*/SIM_MISSED=TRUE/" ./generics.f
endif
endif
I would like the generics.f file changed with that one line change. But it remains the same as the original. The sed command works outside make.
I can't reproduce this using GNU sed 4.2.2 and GNU make 3.82, or at least, I can't reproduce any scenario where the same sed command works from the command line but not in a Makefile.
Simpler Makefile:
all:
# Contrived just so I can test your 2 sed commands.
sed -i -e "s/SIM_MULTI\==[a-z,A-Z]*/SIM_MULTI=TRUE/" ./generics.f
sed -i -e "s/SIM_MISSED\==[a-z,A-Z]*/SIM_MISSED=TRUE/" ./generics.f
Sample file content in generics.f:
SIM_MULTI=foo
SIM_MISSED=bar
Testing:
$ make all
sed -i -e "s/SIM_MULTI\==[a-z,A-Z]*/SIM_MULTI=TRUE/" ./generics.f
sed -i -e "s/SIM_MISSED\==[a-z,A-Z]*/SIM_MISSED=TRUE/" ./generics.f
Confirmed that both sed commands fail to edit a file with this content.
To fix:
Probably, you need to simply remove the \= from your regular expression. The backslash there has no effect, and causes your regex to simply match two equals signs ==. Thus this works:
all:
sed -i 's/SIM_MULTI=[a-zA-Z]*/SIM_MULTI=TRUE/' ./generics.f
sed -i 's/SIM_MISSED=[a-zA-Z]*/SIM_MISSED=TRUE/' ./generics.f
Testing:
$ make all
sed -i 's/SIM_MULTI=[a-zA-Z]*/SIM_MULTI=TRUE/' ./generics.f
sed -i 's/SIM_MISSED=[a-zA-Z]*/SIM_MISSED=TRUE/' ./generics.f
$ cat generics.f
SIM_MULTI=TRUE
SIM_MISSED=TRUE
Further explanation:
There is no need to specify -e there.
There is no need to enclose the script in double quotes, which is riskier because it allows the contents to be modified by the shell.
The bug appears to be \= and I deleted those characters, as mentioned above.
Note that I removed the comma , as well in [a-z,A-Z]. I think that probably isn't what you meant, and it would cause a class of characters including a-z, A-Z and a comma , to be matched by the regex. (And if it is what you mean, you might consider writing it as [a-zA-Z,] as that would be less confusing.)
If this has not resolved your issue, I would need to know things like:
What is the version of your sed.
What is the contents in generics.f.
POSIX/GNU sed have c for "change":
sed -i '/SIM_MULTI=/c\SIM_MULTI=TRUE'
sed -i '/SIM_MISSED=/c\SIM_MISSED=TRUE'

sed will not place variable in to replacment line

I have a very simple sed line to help do instal of Glance for openstack.
sudo sed -i \
's|identity_uri = http://127.0.0.1:35357|identity_uri = http://$MY_PRIVATE_IP:35357|g' \
/etc/glance/glance-api.conf
The part with the $MY_PRIVATE_IP shows up in the config file just as that not the value of 10.0.0.35 which is set in the tty.
If I do an echo you see the correct value.
echo $MY_PRIVATE_IP
10.0.0.35
Not sure what I am missing in the sed statement to make sure the value is inserted in to the config.
Use double quotes in your sed.
sudo sed -i \
"s|identity_uri = http://127.0.0.1:35357|identity_uri = http://$MY_PRIVATE_IP:35357|g" \
/etc/glance/glance-api.conf
I also noticed a different way you could make your variable work. See this question: How to use a bash script variable with sed

grep and/or sed to match a path from a string which has different patterns

I have a big file which is composed of alot of different lines which only have one commen keyword, storaged.
PROC:storage123:0702:2108:0,1,2,3,4,5:storage:vers:storaged:storage123:Storage
123:storage123:-R /etc/orc/storage123 -e emr123#localhost -p Xxx::
PROC:storageabc:0606:2108:0,1,2,3,4,5:storage:vers:storaged:storageabc:Storage
abc:storageabc: -e emabc#localhost -R /etc/orc/storageabc -p 654::
What i need to do is grep for the path that can be found on all storaged keywords that comes after -R. But I only want the path, nothing after that. -R can be found on different places so there is no pattern to it.
I created one espressionen which seemed to work, but I think I made it much for complex (and not 100% sure to match) than it should have to be.
[root:~/scripts/] <conf.txt grep -o 'R *[^ ]*' | grep -o '[^ ]*$' | sed 's/.*R\///'
/etc/orc/storage123
/etc/orc/storagerabc
The espression also is hard to implement in a bash script so something simpler would be great. I need these paths in the script later on.
Cheers
Your attempt is nice, but you can simplify it by using a look-behind:
$ grep -Po '(?<=-R )[^ ]*' file
/etc/orc/storage123
/etc/orc/storageabc
Basically it looks for the string -R (note the space) and from that, it prints everything up to a space.
$ sed 's/.*-R \([^ ]*\).*/\1/' file
/etc/orc/storage123
/etc/orc/storageabc

Find and replace texts in Linux

Can someone tell me the command in Linux to replace the following?
ExecStart=/sbin/runuser -l <USER> -c "/usr/bin/new %i"
PIDFile=/home/new/.new/%H %i.pid
with
ExecStart=/sbin/runuser -l <USER> -c "/usr/bin/vncserver %i"
PIDFile=/home/NUSER/.new/%H %i.pid
I am a little bit confused with sed as I don't know how to insert line break while replacing.
sed -i -r 's#/bin/new#/bin/vncserver#; s#/home/new#/home/NUSER#' file
This might work for you (GNU sed):
sed -r '$!N;s/^(ExecStart=.*)new(.*\nPIDFile=.*\/)new(.*)/\1vnserver\2NUSER\3/;P;D' file
Keep two lines in the pattern space and when the required lines are encountered, replace the two strings.

sed: find and replace string but not super-string

I have a file env which looks like
....
LEGACY_DATABASE_SERVER=10.0.0.1
SERVER=10.1.1.1
and here is my sed command:
sed -e "s/SERVER=.*/SERVER=$INSTANCE_IP/g;n" $ENV_FILE > $ENV_FILE.tmp && mv $ENV_FILE.tmp $ENV_FILE
the problem is that sed is also replacing LEGACY_DATABASE_SERVER which is not what I want. I only want SERVER replaced.
(LEGACY_DATABASE_SERVER is a super string of SERVER and I only want to replace SERVER)
What am I missing?
Presumably, you want to make sure that sed knows "SERVER" is at the beginning of the line:
sed -e "s/^SERVER=.*/SERVER=$INSTANCE_IP/g;n" $ENV_FILE > $ENV_FILE.tmp && mv $ENV_FILE.tmp $ENV_FILE