how to find/replace string in file - linux

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

Related

insert a line with file which has sed command in linux

I am trying to insert line in a file which will have a sed command to insert a line in another file
Like below for eg :
want to add this line sed -i '1s/^/insideFile\n/' secondFile.sh to 65th line of firstfile.txt
I tried:
sed -i '65s/^/\'sed -i \'1s/^/insideFile\n/\' secondFile.sh\'\n/' firstfile.sh
but not able to escape the '
also tried
sed -i "65s/^/'sed -i "1s/^/topLine\n/" $FILE_HOME_LOCAL_FILE'\n/" secondFile.sh
but got
sed: -e expression #1, char 18: unknown option to `s
You may use this sed:
sed -i.bak "65s~^~sed -i '1s/^/insideFile\\\\n/' secondFile.sh\\n~" firstfile.sh
Note:
Use of alternate delimiter ~ to avoid escaping /
use of double quotes for your sed command to avoid escaping single quotes
use of \\\\ to insert a single \
use of \\n inside double quotes to insert a line break
Alternatively with i (insert) instead of s (substitute):
sed -i.bak "65i\\
sed -i '1s/^/insideFile\\\\n/' secondFile.sh\\
" firstfile.sh
Cleanest solution would be to create a sed script like this thus avoiding all quoting and extra escaping:
cat ins.sed
65i\
sed -i '1s/^/insideFile\\n/' secondFile.sh
Then use it as:
sed -i.bak -f ins.sed firstfile.sh
While sed allows you to do these things, it becomes a bit cumbersome when slashes and other control characters are involved. So it might be beneficial to use awk instead for this one:
$ awk '(FNR==65){print "sed -i \0421s/^/insideFile\\n/\042 secondFile.sh"}1' firstfile.txt > firstfile.new
$ mv firstfile.new > firstfile.txt
There are inplace possibilities, but if you come to think of it. Inplace is nothing more than making a temp to rename afterwards. This is exactly what sed is doing.
I want to add this line sed -i '1s/^/insideFile\n/' secondFile.sh to 65th line of firstfile.txt
A scripted solution:
#!/bin/bash
## uncomment to make sure firstfile.txt has at least 65 (e.g. 66) lines
# for index in {1..66}; do
# echo -e "$index" >> firstfile.txt
# done
# prepare the insert text, escaping the newline character twice
text="sed -i '1s/^/insideFile\\\\n/'"
# append to 65th line
sed -i '' "65i\\
$text
" firstfile.txt

SED how to replace Line with Regex

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

How to replace string into numbers using sed?

I am trying to replace string into number from the file
So, I have variable &FLOW which need to change to 001, ex :
cat file.txt
This is file ' PISP &FLOW'
PISD.DATA.CYCLE&FLOW..ONE
desired output
This is file ' PISP 001'
PISD.DATA.CYCLE001.ONE
I tried below commands in a script :
for item in file.txt
do
sed 's/\&FLOW/\./001/g' $item
sed 's/\&FLOW/001/g' $item
done
It is giving error. The second sed command is working, but I need to run first the beginning sed command otherwise after running first the second sed command, it would ignore the beginning sed command.
Any help would be appreciated!
Use a single sed command and use -i to actually modify the file contents and you need to pass file.txt as the input for the sed command:
sed -i 's/&FLOW\.\{0,1\}/001/g' file.txt
See the online demo. If you are using it in Mac OS, you need sed -i '' 's/&FLOW\.\{0,1\}/001/g' file.txt. Also see sed edit file in place.
Pattern details
It is a POSIX BRE compliant pattern matching
&FLOW - a literal &FLOW substring
\.\{0,1\} - 0 or 1 occurrence of a . char.
try this:
for item in file.txt
do
sed 's/\&FLOW\./001/g' $item
sed 's/\&FLOW/001/g' $item
done
You had a redundant / in after FLOW
This might also work:
sed -i 's/\&FLOW[\.]?/001/g' file.txt

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