What's the proper escaping of the following string for find and replace? - linux

I'm trying to use the following instructions
http://www.brunolinux.com/02-The_Terminal/Find_and%20Replace_with_Sed.html
To replace all occurrences of
<BASE href="file://C:\Users\J\Documents\Personal\J\">
with nothing. I have tried
find . -type f -exec sed -i 's/<BASE href=\"file:\/\/C:\\Users\\J\\Documents\\Personal\\J\\">//g' {} \;
But it's not working likely because something is wrong with escaping characters. What have I done wrong?

Changing the substitution delimiter helps a lot:
find . -type f -exec sed -i \
's!<BASE href="file://C:\\Users\\J\\Documents\\Personal\\J\\">!!g' {} \;

try with this:
s/\<BASE href\="file:\/\/C:\\Users\\J\\Documents\\Personal\\J\\\"\>//g
or try with perl with the same same command i.e.,
instead of sed -i use perl -pi

Related

Grep/Sed: How to do a recursive find/replace of a string?

How to find and replace every occurrence of
httpaccess
with
httpaccessabc
in every file of name "access.html" in a particular folder
shopt -s globstar
sed -i.bak 's/httpaccess/&abc/g' **/access.html
Use globstar with ** to match your filename, recursively.
Use sed -i to perform an in-place substitution.
This will create backup files with a suffix .bak. To unset the shell option, use shopt -u globstar afterwards.
find is your friend
find . -type f -name 'access.html' \
-exec sed -i.bak 's/httpaccess/&abc/g' {} \;
Edit
To replace whole pattern use :
find . -type f -name 'access.html' \
-exec sed -i.bak 's/abcde/wazsde/g' {} \;
Notes
Replace . with /your/path of concern.
The \ at the end of first line is just to split the command into two lines for more readability.
The g option with sed s command is for global substitution.
If you know the folder for access.html then :-
sed -i.bak 's/httpaccess/httpaccesabc/g' access.html
(or)
sed -i.bak 's/httpaccess/&abc/g' access.html

Optimal string replacing in files for AIX

I need to remove about 40 emails from several files in a distribution list.
One Address might appear in different files and need to be removed from all of them.
I am working in a directory with several .sh files which also have several lines.
I have done something like this in a couple of test files:
find . -type f -exec grep -li ADDRESS_TO_FIND {} 2>/dev/null \; | xargs sed -i 's/ADDRESS_TO_REMOVE/ /g' *
It works fine but once I try it in the real files, it takes a long time and just sits there. I need to run this in different servers, this is the main cause I want to optimize this.
I have tried to run something like this:
find . -type f -name '*sh' 2>/dev/null | xargs grep ADDRESS_TO_FIND
but that will return:
./FileContainingAddress.sh:ADDRESS_TO_FIND
How do I add something like this:
awk '{print substr($0,1,10)}'
But to return me everything before the ":"?
I can do the rest from there, but haven't found how to trim that part
You can use -exec as a predicate in find, as long as you don't use the multiple file + version, which means that you can provide several -exec clauses each of which will be dependent on the success of the previous one. This style will avoid the construction of lists of filenames, which makes it much more robust in the face of files with odd characters in their names.
For example:
find . -type f -name '*sh' \
-exec grep -qi ADDRESS_TO_FIND {} \; \
-exec sed -i 's/ADDRESS_TO_FIND/ /g' {} \;
You probably want to provide the address as a parameter rather than having to type it twice, unless you really meant for the two instance to be different (ADDRESS_TO_FIND vs. ADDRESS_TO_REMOVE):
clean() {
find . -type f -name '*sh' \
-exec grep -qi "$1" {} \; \
-exec sed -i "s/$1/ /g" {} \;
}
(Watch out for / in the argument to clean. I'll leave making the sed more robust as an exercise.)
After looking back at your question, I noticed something that's potentially quite important:
find -type f -exec grep -li ADDRESS {} \; | xargs sed -i 's/ADDRESS/ /g' *
# here! -----------------------------------------------------------------^
The asterisk is being expanded, so the sed line is operating on every file in the directory.
Assuming that this wasn't a typo in your question, I believe that this is the source of your poor performance. You should remove it!

Rename data using grep?

I am searching for all the instances of DEFAULT_DEV_PATH using grep
grep -r -n --color "DEFAULT_DEV_PATH" *
What I want to do is rename every occurance of DEFAULT_DEV_PATH with just DEV_PATH using command line.
Is that possible ?
Seems like sed might be a better tool for the job...
sed -i 's/DEFAULT_DEV_PATH/DEV_PATH/g' *
And if you wanted to do it recursively like you are doing with grep, you could combine with find:
find . -type f -exec sed -i 's/DEFAULT_DEV_PATH/DEV_PATH/g' {} \;
Per the comments, you may need to provide an argument to the -i option if you are not using a GNU extension.

find & sed: remove lines

I am trying to delete some line in PHP files. I tried to use an find, exec combination:
find . -name '*.php' -exec sed '/#category/d' {} \;
but it only prints out the files contents. Is there anythin wrong in the syntax? Or what is the problem?
Could you try this command:
find . -name '*.php' -exec sed -i '/#category/d' {} \;
I think you've missed -i option
It works, but probably not how you expect.
find . -name '*.php' -exec sed -i '/#category/d' {} \;
Will kill the lines in question.
This should be the command for sed so try to add -i :
sed -i ".bak" '/culpa/d' test.txt
find . -name '*.php' -exec sed -i '/#category/d' {} \;
Source of the answer:
Bash - find a keyword in a file and delete its line

Sed replacement not working when using variables [duplicate]

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' {} \;

Resources