SED how to replace Line with Regex - linux

quick question
I have this line:
regexp: \[apm,(.*)\]apm-(.*)\.pivotal
And I want to be like that:
regexp: \[apm,(.*)\]app-metrics(.*)\.pivotal
The sed command that I'm using and it's not working is that one:
sed -i -E 's/regexp: \\[apm,(.\*)\\\]apm-(.\*)\\.pivotal/regexp: \\[apm,(.\*)\\\]app-metrics(.\*)\\.pivotal/g' FILE_THAT_CONTAINS_THE_LINE

Try to simplify it
sed '/regexp:.*apm.*apm-.*pivotal/s/apm-/&metrics/' FILE...

sed -i -E 's/regexp: \\\[apm,\(\.\*\)\\\]apm-\(.\*\)\\\.pivotal/regexp: \\[apm,(.*)\\]app-metrics(.*)\\.pivotal/' file.txt
you have some escapes missing

Related

How do I replace multiple text at once in a file on Linux or Mac?

So example, I have the file my.txt as below
This is my xzy.
My color is white.
I want to replace "my" to our, and I also want to replace "xyz" to "abc".
How can I do this using a one line command?
Use sed with multiple replacement:
sed -i 's/my/our/g; s/xyz/abc/g' text.txt
With sed
For GNU sed
sed -i -e 's/my/our/g' -e 's/xyz/abc/g' my.txt
In MacOS, -e option may not be available. -i option in MacOS needs an extension. Use something like this:
sed -i.bak 's/my/our/g;s/xyz/abc/g' my.txt

how to find/replace string in file

I'm trying to find in file this string:
"cPHulk":{"BruteForce":3,"Login":3,"*":3}
and replace it with this string:
"cPHulk":{"*":"1","Login":"1","BruteForce":"1"}
I'm using sed to do this but I struggle a lot. My sed command is as follow:
sed -i -e 's/"cPHulk":{"BruteForce":3,"Login":3,"*":3}/"cPHulk":{"*":"1","Login":"1","BruteForce":"1"}/g' /var/cpanel/icontact_event_importance.json
EDIT:
The correct sed formula:
sed -i -e 's/"cPHulk":{"BruteForce":3,"Login":3,"\*":3}/"cPHulk":{"\*":"1","Login":"1","BruteForce":"1"}/g' /var/cpanel/icontact_event_importance.json
I found on some *nix you must provide a blank as the suffix to get it make an in-place replacement:
sed -i '' -e 's/"cPHulk":{"BruteForce":3,"Login":3,"*":3}/"cPHulk":{"*":"1","Login":"1","BruteForce":"1"}/g' /var/cpanel/icontact_event_importance.json
Note the '' after -i

unix sed search replace Section sign §

I want to replace a Section sign "§" in CSV files.
So i try some sed commands, but it did not work for me.
i tried:
sed -i 's/§/\;/g' file.csv
sed -i 's/\§/\;/g' file.csv
Is it a SED or Shell issue?
Thanks!

How to delete lines from file with sed\awk?

I have file, with lines, contains ip with netmask
a.b.c.d/24
w.x.y.z/32
etc
How to delete delete specific row?
i'm using
sed -ie "s#a.b.c.d/24##g" %filname%
but after the removal is an empty string in file.
It should run inside a script, with ip as parameter and also work in freebsd under sh.
Sed solution
sed -i '/<pattern-to-match-with-proper-escape>/d' data.txt
-i option will change the original file.
Awk solution
awk '!/<pattern-to-match-with-proper-escape>/' data.txt
Using sed:
sed -i '\|a.b.c.d/24|d' file
Command line arg:
For the input being command line argument, say 1st argument($1):
sed -i "\|$1|d" file
Replace $1 with appropriate argument number as is your case.
You should use d (delete) not g. Also do not use s (replacement).
sed -ie '/a.b.c.d\/24/d' %filename%
In a script you should using it in this way
IP=$1
IPA=${IP////\\/}
sed -i /"${IPA}"/d %filename%
And the script parameter should be called in this way:
./script.sh a.b.c.d/24
perl -i -lne 'print unless(/a.b.c.d\/24/)' your_file
or in awk if you donot want to do inplace editing:
awk '$0!~/a.b.c.d\/24/' your_file

linux shell sed command

I have file sedFile.txt which has string in format CONNECTION='mysql://user:user#10.79.19.2:3308/SSMS/SUBSCRIBE';
I created one script which has following lines:
fin=CONNECTION='mysql://user:user#10.79.19.2:3308/SSMS/SUBSCRIBE';
repla=connection
sed -i "s/\$fin/$repla/g" /home/sedFile.txt
Even though the script is running, it's not doing changes in my file.
I tried following:
sed -i 's/${fin}/${repla}/g' /home/sedFile.txt
sed -i 's/^$fin/$repla/g' /home/sedFile.txt
sed -i "s/$fin/$repla/g" /home/sedFile.txt
sed -i "s/${fin}/${repla}/g" /home/sedFile.txt
If you want the single quotes to be included in the pattern you have to quote or escape them:
fin="CONNECTION='mysql://user:user#10.79.19.2:3308/SSMS/SUBSCRIBE'"
then, use any of the four lines you tried (i.e. not the one with \$fin).
Update: In order to make sed work, you cannot use / to separate the pattern and the substitution, because this character exists in the string already. Use a different separator:
sed -i "s,$fin,$repla,g" /home/sedFile.txt
Might be the same as the other answers, but I doesn't hurt to try
fin="CONNECTION='mysql://user:user#10.79.19.2:3308/SSMS/SUBSCRIBE';"
repla="connection"
sed -i "s|${fin}|${repla}|g" /home/sedFile.txt
fin="CONNECTION='mysql://user:user#10.79.19.2:3308/SSMS/SUBSCRIBE'"
repla=connection
in=$fin out=$repla perl -pi.nk -e 's/\Q$ENV{"in"}/$ENV{"out"}/g' /home/sedFile.txt

Resources