Sed issue (regex syntax) - sed

I'm trying this sed to replace strings:
find . -type f -name "*.php" -exec sed -i "s|\!preg_match(\"\!modules\.php\!\", \\\$_SERVER\['PHP_SELF'\]) \{|\!preg_match(\"\!modules\\\.php\!\", \\\$_SERVER\['PHP_SELF'\])) \{|g" {} ";"
But I've an error message (error in pairing {)
I can't find the good syntax to do that. I just want to add a round bracket at the end on all files...
[edit]
Ok found it...
find . -type f -name "*.php" -exec sed -i "s|\!preg_match(\"\!modules\\\.php\!\", \\\$_SERVER\['PHP_SELF'\]) {|\!preg_match(\"\!modules\\\.php\!\", \\\$_SERVER\['PHP_SELF'\])) {|g" {} ";"

Use single quotes rather than double quotes to escape regexes and other programming language constructs that you pass on the command line from bash...

with simple quote and assuminf $SERVER is litteral
sed -i 's|!preg_match("!modules\.php!", \$_SERVER\['"'"'PHP_SELF'"'"'\]) {|!preg_match("!modules\.php!", \$_SERVER\['"'"'PHP_SELF'"'"'\])) {|g'
I remove the !, normaly ! should pass without escape
i propose also the simplification
sed -i 's|\(!preg_match("!modules\.php!", \$_SERVER\['"'"'PHP_SELF'"'"'\])\) {|\1) {|g'

Related

sed remove a special control character from many files

Can someone please give me the exact syntax for removing ^# from thousands of html files in nested directories using sed? The ^# is a control character inserted by a windows program that generated these files. I cannot seem to get the syntax right.
I tried this (but it did not work) using a file since I could not enter the control-character at the command prompt:
find ./ *.html -type f -exec sed -i 's/^#//g' {} ;
POSIX sed doesn't handle NUL in input but GNU sed can with hex escape:
find . -name '*.html' -type f -exec sed -i 's/\x0//g' '{}' +

"invalid command code ." error from sed after running find and sed on Mavericks

I run the following command to find and replace an old web address to a new one.
find . -type f -print0 | xargs -0 sed -i \
's/http:\/\/www\.oldwebaddress\.com\/techblog/https:\/\/github\.com\/myname/g'
However I get the following error.
sed: 1: "./.DS_Store": invalid command code .
I tried this one after reading some of Stack Overflow posts but didn't work either.
find . -type f -print0 | xargs -0 sed -i "" \
's/http:\/\/www\.oldwebaddress\.com\/techblog/https:\/\/github\.com\/myname/g'
sed: RE error: illegal byte sequence
What am I doing wrong here?
The sed on Mac OS X accepts the -i option but requires an argument, the suffix to use for the backup files. It is not optional as it is with GNU sed. So, the sed script tries to use your s/// command as the suffix, and then the first file name didn't happen to be a valid sed command.
For the second attempt, with -i "", it is not quite so clear what's up. I assume there's either a backslash after the "" or the whole lot is really on one line, so that it is syntactically correct.
It is simpler to use some character other than / as the separator when editing path names. Often, % works:
-e 's%http://www\.oldwebaddress\.com/techblog%https://github\.com/myname%g'
but you can use any character; Control-A or Control-G are quite effective too and even more unlikely to appear in a URL than %.
It is not clear to me, though, why you're getting the RE error (invalid byte sequence). Copying and pasting from the question doesn't show a problem, and the other question referenced suggests LANG=C LC_CTYPE=C but I'm not running into problems with LANG=en_US.UTF-8 and nothing set for LC_CTYPE.
Does this help?
Also if you will just do -print instead of -print0 then you can replace xargs -0 with just xargs.
You might also consider using perl instead of BSD sed.
find . -type f -print | xargs perl -pi.bak -e 's/http\:\/\/www\.oldwebaddress\.com\/techblog/https\:\/\/github\.com\/myname/g'
Above should do the replacements and save all files backups adding .bak at the end of filenames. Use perl -pi -e if you don't want backups to be created.

Global, recursive rename with sed fails

I am attempting to search through all python files recursively down into all subdirectories and replace the word organizations with organisations. Like this:-
$ find . -name "*.py" | xargs sed -i 's/organizations/organisations/g'
sed: 1: "./bookings/__init__.py": invalid command code .
But I get an error message. How do I do this right?
Ok. Figured it out. I am using Mac OSX's sed, which requires that I add a "" before the s/foo/foo_bar/g string.
So
$ find . -name "*.py" | xargs sed -i "" 's/organizations/organisations/g'
worked.

How to find and replace all occurrences of a string recursively in a directory tree? [duplicate]

This question already has answers here:
How can I do a recursive find/replace of a string with awk or sed?
(37 answers)
Closed 1 year ago.
Using just grep and sed, how do I replace all occurrences of:
a.example.com
with
b.example.com
within a text file under the /home/user/ directory tree recursively finding and replacing all occurrences in all files in sub-directories as well.
Try this:
find /home/user/ -type f | xargs sed -i 's/a\.example\.com/b.example.com/g'
In case you want to ignore dot directories
find . \( ! -regex '.*/\..*' \) -type f | xargs sed -i 's/a\.example\.com/b.example.com/g'
Edit: escaped dots in search expression
Try this:
grep -rl 'SearchString' ./ | xargs sed -i 's/REPLACESTRING/WITHTHIS/g'
grep -rl will recursively search for the SEARCHSTRING in the directories ./ and will replace the strings using sed.
Ex:
Replacing a name TOM with JERRY using search string as SWATKATS in directory CARTOONNETWORK
grep -rl 'SWATKATS' CARTOONNETWORK/ | xargs sed -i 's/TOM/JERRY/g'
This will replace TOM with JERRY in all the files and subdirectories under CARTOONNETWORK wherever it finds the string SWATKATS.
On macOS, none of the answers worked for me. I discovered that was due to differences in how sed works on macOS and other BSD systems compared to GNU.
In particular BSD sed takes the -i option but requires a suffix for the backup (but an empty suffix is permitted)
grep version from this answer.
grep -rl 'foo' ./ | LC_ALL=C xargs sed -i '' 's/foo/bar/g'
find version from this answer.
find . \( ! -regex '.*/\..*' \) -type f | LC_ALL=C xargs sed -i '' 's/foo/bar/g'
Don't omit the Regex to ignore . folders if you're in a Git repo. I realized that the hard way!
That LC_ALL=C option is to avoid getting sed: RE error: illegal byte sequence if sed finds a byte sequence that is not a valid UTF-8 character. That's another difference between BSD and GNU. Depending on the kind of files you are dealing with, you may not need it.
For some reason that is not clear to me, the grep version found more occurrences than the find one, which is why I recommend to use grep.
I know this is a really old question, but...
#vehomzzz's answer uses find and xargs when the questions says explicitly grep and sed only.
#EmployedRussian and #BrooksMoses tried to say it was a dup of awk and sed, but it's not - again, the question explicitly says grep and sed only.
So here is my solution, assuming you are using Bash as your shell:
OLDIFS=$IFS
IFS=$'\n'
for f in `grep -rl a.example.com .` # Use -irl instead of -rl for case insensitive search
do
sed -i 's/a\.example\.com/b.example.com/g' $f # Use /gi instead of /g for case insensitive search
done
IFS=$OLDIFS
If you are using a different shell, such as Unix SHell, let me know and I will try to find a syntax adjustment.
P.S.: Here's a one-liner:
OLDIFS=$IFS;IFS=$'\n';for f in `grep -rl a.example.com .`;do sed -i 's/a\.example\.com/b.example.com/g' $f;done;IFS=$OLDIFS
Sources:
Bash: Iterating over lines in a variable
grep(1) - Linux man page
Official Grep Manual
sed(1) - Linux man page
Official sed Manual
For me works the next command:
find /path/to/dir -name "file.txt" | xargs sed -i 's/string_to_replace/new_string/g'
if string contains slash 'path/to/dir' it can be replace with another character to separate, like '#' instead '/'.
For example: 's#string/to/replace#new/string#g'
We can try using the more powerful ripgrep as
rg "BYE_BYE_TEXT" ./ --files-with-matches | xargs sed -i "s/BYE_BYE_TEXT/WELCOME_TEXT/g"
Because ripgrep is good at finding and sed is great at replacing.
it is much simpler than that.
for i in `find *` ; do sed -i -- 's/search string/target string/g' $i; done
find i => will iterate over all the files in the folder and in subfolders.
sed -i => will replace in the files the relevant string if exists.
Try this command:
/home/user/ directory - find ./ -type f \
-exec sed -i -e 's/a.example.com/b.example.com/g' {} \;
The command below will search all the files recursively whose name matches the search pattern and will replace the string:
find /path/to/searchdir/ -name "serachpatter" -type f | xargs sed -i 's/stringone/StrIngTwo/g'
Also if you want to limit the depth of recursion you can put the limits as well:
find /path/to/searchdir/ -name "serachpatter" -type f -maxdepth 4 -mindepth 2 | xargs sed -i 's/stringone/StrIngTwo/g'

Odd Sed Error Message

bash-3.2$ sed -i.bakkk -e "s#/sa/#/he/#g" .*
sed: .: in-place editing only works for regular files
I try to replace every /sa/ with /he/ in every dot-file in a folder. How can I get it working?
Use find -type f to find only files matching the name .* and exclude the directories . and ... -maxdepth 1 prevents find from recursing into subdirectories. You can then use -exec to execute the sed command, using a {} placeholder to tell find where the file names should go.
find . -type f -maxdepth 1 -name '.*' -exec sed -i.bakkk -e "s#/sa/#/he/#g" {} +
Using -exec is preferable over using backticks or xargs as it'll work even on weird file names containing spaces or even newlines—yes, "foo bar\nfile" is a valid file name. An honorable mention goes to find -print0 | xargs -0
find . -type f -maxdepth 1 -name '.*' -print0 | xargs -0 sed -i.bakkk -e "s#/sa/#/he/#g"
which is equally safe. It's a little more verbose, though, and less flexible since it only works for commands where the file names go at the end (which is, admittedly, 99% of them).
Try this one:
sed -i.bakkk -e "s#/sa/#/he/#g" `find .* -type f -maxdepth 0 -print`
This should ignore all directories (e.g., .elm, .pine, .mozilla) and not just . and .. which I think the other solutions don't catch.
The glob pattern .* includes the special directories . and .., which you probably didn't mean to include in your pattern. I can't think of an elegant way to exclude them, so here's an inelegant way:
sed -i.bakkk -e "s$/sa/#/he/#g" $(ls -d .* | grep -v '^\.\|\.\.$')