This question already has answers here:
How to insert strings containing slashes with sed? [duplicate]
(11 answers)
Closed 5 years ago.
How can i use the sed to replace string containing backslash "\" is all files of specific directory.
I tried this but wont work for me
find /home/tds/nfb -type f -exec sed -i 's//var/www/tds///home/tds//' {} \;
I want to replace "/var/www/tds/" with "/home/tds/"
You can do
find /home/tds/nfb -type f -exec sed -i 's|/var/www/tds/|/home/tds/|' {} \;
where the delimiter / is replaced by |. (sed can use almost any character as a delimiter -- it picks whatever character follows the s). Alternatively, you could escape all your backslashes as follows and still used \:
sed -i 's/\/var\/www\/tds\//\/home\/tds\//'
Related
This question already has answers here:
BASH: recursive program to replace text in a tree of files
(3 answers)
Closed 5 years ago.
From a given folder, I want to search recursively accross all subfolders searching for files with name file.txt replacing all occurences of Foo -case sensitive-
with Bar.
Which is the simplest way of achieving this with basic scripting (Bash / sed / grep / find...).
find + sed solution:
find . -type f -name "file.txt" -exec sed -i 's/Foo/Bar/g' {} \;
This question already has answers here:
How to insert strings containing slashes with sed? [duplicate]
(11 answers)
Closed 5 years ago.
i need a quick command (linux or windows) to replace every \\ with a /, and all tries with sed failed because of the /.
(I already tried find . -name '*.*' -exec sed -i 's/\\///g' {} \;, but i think it failed with the "/".
find . -name '*.*' -type f -exec sed -i 's:\\\\:/:g' {} \;
You need to escape each backslash, and using a colon or comma as separators is generally recommended when making replacements with forward-slash. However, escaping the forward slash works too:
find . -name '*.*' -type f -exec sed -i 's/\\\\/\//g' {} \;
As pointed out in comments the OS module is probably what you really need to look at.
Edit: thanks to #tripleee for reminding me of the -type f line, which limits it to files, rather than including the current directory.
Also, I copied the syntax *.* from the OP but in general it isn't helpful. * alone is usually what you want, since files aren't guaranteed to have a dot in their name. Assuming you were happy to include files not containing a dot, the simplest thing to do here is have no -name at all:
find . -type f -exec sed -i 's:\\\\:/:g' {} \;
This question already has answers here:
Linux command: How to 'find' only text files?
(16 answers)
Closed 6 years ago.
I tried using this:
$ find . -type f -exec file {} \;
./alma: ASCII text
./jaj.C: C source, ASCII text
./repa: ASCII text, with escape sequences
./mas.cpp: C++ source, ASCII text
./capa: ASCII text
./valami: ASCII text
But if it's a cpp file for example it still writes text so I can't use grep to exclude binary files....what should I do?
Here's the fast method to do it:
find . -type f -exec grep -Iq . {} \; -print
-I in grep will ignore binary files, text files will match right away because of . (any character match), grep will give success for matched file, so -print from find will print the filename.
I'm trying to find \STRING1\ and replace it with \STRING2\ (mind the backslashes)
I tried the following:
find ./ -type f -exec sed -i "s/\\STRING1\\/\\STRING2\\/g" {} \;
It didn't work and I got this error: sed: -e expression #1, char 21: unterminated `s' command
I searched the web and people recommend adding two backslashes to any backslash I'd like to search and/or replace. So I tried this:
find ./ -type f -exec sed -i "s/\\\STRING1\\\/\\\STRING2\\\/g" {} \;
I got no errors this time, but it didn't do anything. What's the issue?
Use single quotes:
> echo "some text\STRING1\ and some more \STRING1\andeven more" | sed 's/\\STRING1\\/\\STRING2\\/g'
some text\STRING2\ and some more \STRING2\andeven more
If you want to quote your sed command with double quotes, then you have to use quadruple backslashes to get a single literal backslash in the pattern:
find ./ -type f -exec sed -i "s/\\\\STRING1\\\\/\\\\STRING2\\\\/g" {} \;
This is because backslash is a live metacharacter for double-quoted shell strings, so the shell cuts the number of backslashes in half and then so does sed. Alternatively, use single quotes as suggested in the other answer.
You need to use a single quoted string.
If you use double quotes, the string is preprocessed by your shell and sed receives only single backslashes.
This question already has answers here:
Replace a string in shell script using a variable
(12 answers)
Closed 9 years ago.
something strange is happening when trying to replace string with sed. This works :
find /home/loni/config -type f -exec sed -i 's/some_pattern/replacement/g' {} \;
So it works when I manually type the strings. But in the case below replacement doesn't occur :
find /home/loni/config -type f -exec sed -i 's/${PATTERN}/${REPLACEMENT}/g' {} \;
When I echo these two variables PATTERN and REPLACEMENT they have the correct values.
I'm trying to replace all occurences of pattern string with replacement string in all files in my config directory.
Try
find /home/loni/config -type f -exec sed -i "s/${PATTERN}/${REPLACEMENT}/g" {} \;
instead. The ' quotes don't expand variables.
Not sure if I got this right, but if you want to replace the ${PATTERN} with ${REPLACEMENT} literally you have to escape the dollar and maybe the braces, those are reserved characters in regular expressions:
find /home/loni/config -type f -exec sed -i -e 's/\$\{PATTERN\}/\$\{REPLACEMENT\}/g' {} \;