Search and Replace SED Linux - linux

I need to convert my all url relative path
https://www.example.com/image/abc.gif to /image/abc.gif
i tried this command but not worked for https:\\ section . How can i use https\\ in this command .
grep -rl "http://www.example.com" /root/ | xargs sed -i 's/http://www.example.com//g'

The following will do the job:
echo "https://www.example.com/image/abc.gif" | sed 's/https:\/\/www\.example\.com//g'
OUTPUT
/image/abc.gif

You can use regular expressions, the -r indicates extended regexes. The ? says 0 or 1 occurrence of 's'.
echo "http://www.example.com/image/abc.gif" | sed -r 's/https?:\/\/www\.example\.com//g'

Related

Search & replace on all files in server

I want to search for this line:
<script type="text/javascript" src="//example.com/file.php?zoneid=112" async data-cfasync="false"></script>
And remove it/replace it with blank.
What command line could I run on linux to search and replace this term on all files on the server?
I have used a combination of grep and sed
string_to_replace='<script type="text/javascript" src="//example.com/file.php?zoneid=112" async data-cfasync="false"></script>'
First just run the grep statement to make sure you are matching the right string and file. If everything looks good, then run the following command;
grep -Inr "${string_to_replace}" | while read line; do filename=`echo $line | cut -d ":" -f 1`; line_no=`echo $line | cut -d ":" -f 2`; sed "${line_no}d" -i $filename; done
In grep, -I will ignore binaries, -n will print the line number and -r will do a recursive search.
Let me know if something is not working.
You can use sed with regex and command d over the file:
sed -i '/regex/d' file
Next construction could help you to pass filenames with find to sed:
find . | xargs sed -i '/regex/d'
Be careful with debugging. :)

How to apply my sed command to some lines of all my files?

I've 95 files that looks like :
2019-10-29-18-00/dev/xx;512.00;0.4;/var/x/xx/xxx
2019-10-29-18-00/dev/xx;512.00;0.68;/xx
2019-10-29-18-00/dev/xx;512.00;1.84;/xx/xx/xx
2019-10-29-18-00/dev/xx;512.00;80.08;/opt/xx/x
2019-10-29-18-00/dev/xx;20480.00;83.44;/var/x/x
2019-10-29-18-00/dev/xx;3584.00;840.43;/var/xx/x
2019-10-30-00-00/dev/xx;2048.00;411.59;/
2019-10-30-00-00/dev/xx;7168.00;6168.09;/usr
2019-10-30-00-00/dev/xx;3072.00;1036.1;/var
2019-10-30-00-00/dev/xx;5120.00;348.72;/tmp
2019-10-30-00-00/dev/xx;20480.00;2033.19;/home
2019-10-30-12-00;/dev/xx;5120.00;348.72;/tmp
2019-10-30-12-00;/dev/hd1;20480.00;2037.62;/home
2019-10-30-12-00;/dev/xx;512.00;0.43;/xx
2019-10-30-12-00;/dev/xx;3584.00;794.39;/xx
2019-10-30-12-00;/dev/xx;512.00;0.4;/var/xx/xx/xx
2019-10-30-12-00;/dev/xx;512.00;0.68;/xx
2019-10-30-12-00;/dev/xx;512.00;1.84;/var/xx/xx
2019-10-30-12-00;/dev/xx;512.00;80.08;/opt/xx/x
2019-10-30-12-00;/dev/xx;20480.00;83.44;/var/xx/xx
2019-10-30-12-00;/dev/x;3584.00;840.43;/var/xx/xx
For some lines I've 2019-10-29-18-00/dev and for some other lines, I've 2019-10-30-12-00;/dev/
I want to add the ; before the /dev/ where it is missing, so for that I use this sed command :
sed 's/\/dev/\;\/dev/'
But How I can apply this command for each lines where the ; is missing ? I try this :
for i in $(cat /home/xxx/xxx/xxx/*.txt | grep -e "00/dev/")
do
sed 's/\/dev/\;\/dev/' $i > $i
done
But it doesn't work... Can you help me ?
Could you please try following with GNU awkif you are ok with it.
awk -i inplace '/00\/dev\//{gsub(/00\/dev\//,"/00;/dev/")} 1' *.txt
sed solution: Tested with GNU sed for few files and it worked fine.
sed -i.bak '/00\/dev/s/00\/dev/00\;\/dev/g' *.txt
This might work for you (GNU sed & parallel):
parallel -q sed -i 's#;*/dev#;/dev#' ::: *.txt
or if you prefer:
sed -i 's#;*/dev#;/dev#' *.txt
Ignore lines with ;/dev.
sed '/;\/dev/{p;d}; s^/dev^;/dev^'
The /;\/dev/ check if the line has ;/dev. If it has ;/dev do: p - print the current line and d - start from the beginning.
You can use any character with s command in sed. Also, there is no need in escaping \;, just ;.
How I can apply this command for each lines where the ; is missing ? I try this
Don't edit the same file redirecting to the same file $i > $i. Think about it. How can you re-write and read from the same file at the same time? You can't, the resulting file will be in most cases empty, as the > $i will "execute" first making the file empty, then sed $i will start running and it will read an empty file. Use a temporary file sed ... "$i" > temp.txt; mv temp.txt "$i" or use gnu extension -i sed option to edit in place.
What you want to do really is:
grep -l '00/dev/' /home/xxx/xxx/xxx/*.txt |
xargs -n1 sed -i '/;\/dev/{p;d}; s^/dev^;/dev^'
grep -l prints list of files that match the pattern, then xargs for each single one -n1 of the files executes sed which -i edits files in place.
grep for filtering can be eliminated in your case, we can accomplish the task with a single sed command:
for f in $(cat /home/xxx/xxx/xxx/*.txt)
do
[[ -f "$f" ]] && sed -Ei '/00\/dev/ s/([^;])(\/dev)/\1;\2/' "$f"
done
The easiest way would be to adjust your regex so that it's looking a bit wider than '/dev/', e.g.
sed -i -E 's|([0-9])/dev|\1;/dev|'
(note that I'm taking advantage of sed's flexible approach to delimiters on substitute. Also, -E changes the group syntax)
Alternatively, sed lets you filter which lines it handles:
sed -i '/[0-9]\/dev/ s/\/dev/;/dev/'
This uses the same substitution you already have but only applied on lines that match the filter regex

Exclude smililar string while using sed

I have a some scripts into a directory and this directory containing other folders also. Which ever script are residing in my first (parent) directory I want to replace all 'dev' string into prod. but in some of the script I am nullifying the out by using dev/null . I don't want to change this as prod/null.
I am replacing all other 'dev' using below command.
grep -rl dev somedir|xargs sed -i 's/dev/prod/g'
Please let me know if there is any way to exclude changing dev/null to prod/null
Thanks.
If each entry in grep -r1 dev somedir is separated by a newline, you can use this
grep -rl dev somedir | grep -v "dev/null" | xargs sed -i 's/dev/prod/g'
It is possible to replace /dev/null to some other string (i.e. /xxx/null) before doing dev -> prog substitution and change it to /dev/null later.
grep -rl dev somedir|xargs sed -i -e 's!/dev/null!/xxx/null!g' -e 's/dev/prod/g' -e 's!/xxx/null!/dev/null!g'

Replace string using grep and sed

I have bunch of files in a directory,
I need to change prefix of lines in file like "AB_" to "YZ_"
how can i do it?
i have used grep and sed like,
grep -nr "AB_" ./ | xargs -0 sed -i 's/AB_/YZ_/g'
but giving error,
: File name too long
example string in a file are: Hello AB_WORLD! and Hello WORLD_AB!
Thanks.
sed will take multiple files as arguments, so this should work:
sed -i '/AB_/s//YZ_/g' *
(Note that -i is non-standard)
You mean grep -lr not grep -nr
-l gives you the file name; -n gives you the matching line with line number prepended
I like Perl for this one:
The -i option will save the original file with a.bak extension.
$ perl -i.bak -pe 's/^AB_/YZ_/' *.txt
grep -lr "AB_" ./ | while read file
do
echo "Change file $file ..."
sed -i 's/AB_/YZ_/g' ${file}
done
sed one-liner answer
Find php files in the directory containing string "foo" and replace all occurences with "bar"
grep -l foo *.php | xargs sed -i '' s/foo/bar/g
To recurse through directories
grep -rl foo * | xargs sed -i '' s/foo/bar/g
(just done successfully on 8100 files)
grep -rl bar * | wc -l
8102

Linux - replace a string

I need to replace a string / line for each instance of that string / line.
In the following line:
set output "/DS/tmp/2.gnuplot.ps"
I need it to be:
set output "./gnuplot.ps"
I want to use:
grep -rl 'stringONE' ./ | xargs sed -i 's/stringONE/stringTWO/g'
but when I try to use it, it has a conflict with the / in the string...
grep -rl '/DS/tmp/2.gnuplot.ps' ./ | xargs sed -i 's//DS/tmp/2.gnuplot.ps/./gnuplot.ps/g'
Any help will be greatly appreciated!!!
If the string to be replaced contains slash (/) then you can delimit arguments of sed's s command with something else, in this case I used comma:
grep -rl '/DS/tmp/2.gnuplot.ps' ./|xargs sed -i 's,/DS/tmp/2.gnuplot.ps,./gnuplot.ps,g'

Resources