...
tomcat.javaoptions=-Djava.net.preferIPv4Stack\=true \
-Djava.net.preferIPv6Addresses\=false \
-Dcom.sun.management.jmxremote.port\=12345 \
-Djava.rmi.server.hostname=${application.hostname}
...
I need add new line to the end of tomcat.javaoptions with sed. I have to use regex, because I do not know how java options will look originally. i know only that it starts from tomcat.javaoptions= and can have multiple lines. Any idea?
EDITED:
I need to add new line
...
tomcat.javaoptions=-Djava.net.preferIPv4Stack\=true \
-Djava.net.preferIPv6Addresses\=false \
-Dcom.sun.management.jmxremote.port\=12345 \
-Djava.rmi.server.hostname=${application.hostname} \
-agentpath:/opt/agent/agent.so,name=agent
...
I tried it just to add only "-agentpath" but no luck
sed -i "/^tomcat.javaoptions=(.*/n*)*/s/$/ \\\\\n -agentpath/g" file
I don't know what the end of tomcat.javaoptions condition is, but I modify your script, so it works:
sed -r -i -e "/^tomcat.javaoptions=(.*\n*)*/s/$/ \n -agentpath/g" File
Changes:
Remember to add -r parameter to sed,
replace your /n with \n.
Related
I'm hoping there's someone that could help with this. I'm most certain the question have been asked however, i'm having some difficulty understanding some of the answers.
I have the following text in file.txt
value1=192.168.1.2
value2=10.1.1.15
I'd like to replace those ip address and add value3=10.224.100.5 if value3 doesn't exist, using sed.
What i have so far or at least tried.
sed \
-e '/^#\?\(\s*value1\s*=\s*\).*/{s//\newvalue/;:a;n;ba;q}' \
-e '$avalue1=newvalue' \
-e '/^#\?\(\s*value2\s*=\s*\).*/{s//\newvalue/;:a;n;ba;q}' \
-e '$avalue2=newvalue' \
-e '/^#\?\(\s*value3\s*=\s*\).*/{s//\newvalue/;:a;n;ba;q}' \
-e '$avalue3=newvalue' file.txt
This works fine if value(1,2,3) doesn't exist however, if value1 exists in file.txt, it stops at 1.
I'm assuming its because of the ;q
Any advice please? i'm really having a hard time getting this.
One way with awk:
awk -v v1="new1" -v v2="new2"
'BEGIN{FS=OFS="=";addV3=1}
$1=="value1"{$2=v1}
$1=="value2"{$2=v2}
$1=="value3"{addV3=0}7;
END{if(addV3)print "value3=newV3"}' file
test with your example:
kent$ cat f
value1=192.168.1.2
value2=10.1.1.15
kent$ awk -v v1="new1" -v v2="new2" 'BEGIN{FS=OFS="=";addV3=1}$1=="value1"{$2=v1}$1=="value2"{$2=v2}$1=="value3"{addV3=0}7;END{if(addV3)print "value3=newV3"}' f
value1=new1
value2=new2
value3=newV3
I am trying to add a line in a file using sed after first match. I looked my threads online, addressing the same issue and tried below versions of the command:
sudo sed -e '/cfg_file/cfg_file='$NEW_FILE -e '/cfg_file/cfg_file=/q' MAIN_CONF_FILE
sudo sed -i '0,/cfg_file/ a\cfg_file='$NEW_FILE $MAIN_CONF_FILE
sudo sed -i '1s/cfg_file/cfg_file='$NEW_FILE $MAIN_CONF_FILE
sudo sed -i '1/cfg_file/s/cfg_file='$NEW_FILE $MAIN_CONF_FILE
Unfortunately, nothing worked for me. Either they show error, in case of point 3, or show similar behavior of adding lines after each match.
SAMPLE FILE
cfg_file=some_line1
cfg_file=some_line2
Now I want to add a line after first match of cg_file, like below.
EXPECTED RESULT
cfg_file=some_line1
cfg_file=my_added_line_after_first_match_only.
cfg_file=some_line2
Help me in adding line after first match and correcting my command.
Since you're on Ubuntu, you are using GNU sed. GNU sed has some weird features and some useful ones; you should ignore the weird ones and use the useful ones.
In context, the useful one is ranges starting at line 0. Weird ones are the way it messes with a, i and c commands.
MAIN_CONF_FILE=/tmp/copy.of.main.config.file
NEWFILE="my_added_line_after_first_match_only."
sed -e '0,/^cfg_file=/ { /^cfg_file/ a\' \
-e "cfg_file=$NEWFILE" \
-e '}' \
"$MAIN_CONF_FILE"
In classic sed, the a command is followed by backslash-newline, and each subsequent line of the script is appended up to and including the first line without a backslash at the end (and the backslash is removed). Each -e argument functions as a line in the script. Distinguish between the shell lines escaped with backslash at the end and the sed script lines with backslash at the end.
Example at work
$ cat /tmp/copy.of.main.config.file | so
cfg_file=some_line1
cfg_file=some_line2
$ cat script.sh
MAIN_CONF_FILE=/tmp/copy.of.main.config.file
NEWFILE="my_added_line_after_first_match_only."
SED=/opt/gnu/bin/sed
${SED} \
-e '0,/^cfg_file=/ { /^cfg_file/ a\' \
-e "cfg_file=$NEWFILE" \
-e '}' \
"$MAIN_CONF_FILE"
$ bash script.sh
cfg_file=some_line1
cfg_file=my_added_line_after_first_match_only.
cfg_file=some_line2
$
This is based on your attempt 2, but avoids some of the weird stuff.
Basic sanity
As I noted, it is not sensible to experiment with sudo and the -i option to sed. You don't use those until you know that the script will do the job correctly. It is dangerous to do anything as root via sudo. It is doubly dangerous when you don't know whether what you're trying to use will work. Don't risk wrecking your system.
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
I'm requesting help with a very simple script...
#!/usr/bin/sed -f
sed '/11,yahoo/d'
sed '/2506,stackover flow/d'
sed '/2536,reddit/d'
Just need it to remove three matches that account for 18408 in my file, data.csv
% sed -f remove.sed < data.csv
sed: 3: remove.sed: unterminated substitute pattern
Doing these same lines individually is no problem at all, so what am I doing wrong with this?
Using freeBSD 10.1 and its implementation of sed, if that matters.
This, being a sed script, should not have "sed" at each line.
Either change it to:
#!/usr/bin/sed -f
/11,yahoo/d
/2506,stackover flow/d
/2536,reddit/d
Or to
#!/bin/sh
sed -e /11,yahoo/d \
-e /2506,stackover flow/d \
-e /2536,reddit/d
I want to replace
ExecStart=/sbin/runuser -l <USER> -c "/usr/bin/vncserver %i"
PIDFile=/home/<USER>/.vnc/%H %i.pid
with
ExecStart=/sbin/runuser -l root -c "/usr/bin/vncserver %i"
PIDFile=/root/.vnc/%H %i.pid
I have tried:
sed -i 's$ExecStart=/sbin/runuser -l <USER> -c "/usr/bin/vncserver %i" \n PIDFile=/home/<USER>/.vnc/%H %i.pid$ExecStart=/sbin/runuser -l root -c "/usr/bin/vncserver %i" \n PIDFile=/root/.vnc/%H %i.pid$g' file
I used $ instead of / for special characters. But that doesn't work, nothing is getting edited.
Please tell me what I am missing. Thx.
Got the Soution.Thx to the answerer :) :
sed -i 's$ExecStart=/sbin/runuser -l <USER> -c "/usr/bin/vncserver %i"$ExecStart=/sbin/runuser -l root -c "/usr/bin/vncserver %i"$g' uu && sed -i 's$PIDFile=/home/<USER>/.vnc/%H %i.pid$PIDFile=/root/.vnc/%H %i.pid$g' uu
sed primarily works on one line at a time, so you'll need to define two separate substitute operations, one for each line:
sed -e 's/-l <USER> -c/-l root -c/' \
-e 's%home/<USER>%root%' \
file
Note that the character after s is the delimiter for the regular expression. Rather than messing with backslashes, I changed to % in the second to avoid the collision with slashes.
If you're really worried, you can make your matches match more, but what I showed is unlikely to fail you. It is odd that you want /root to replace /home/<USER>. You might do better with a marker like <HOME> which can be set correctly.