UNIX find and replace using execdir and bash - regex

I am new to scripting and have a requirement where I need to change the special characters from file and replace with some other character.
Below is the file name where I have to replace the ? by _.
file - 21041159?74DECL?ARAÇÃO14581?5904289?6770700.pdf
result - 21041159_74DECL_ARAÇÃO14581_5904289_6770700.pdf
find . -depth -name '*\?*' -type f -execdir bash -c 'mv "$1" "${1/\?/_}"' -- {} \;
The above script changes the first occurrence of question mark to underscore but not from complete file name.
Please suggest what can be done?

A simplified version of your question is:
I can replace the first occurrence of a string in a bash variable with ${var/foo/bar}.
How can I replace all occurrences?
And the answer is to use double slash: ${var//foo/bar}.
In context, it would be:
find . -depth -name '*\?*' -type f -execdir bash -c 'mv "$1" "${1//\?/_}"' -- {} \;
# Here --^

Related

Using find/sed to replace strings in text files- works only on some of the matches

I want to replace
{not STRING }
with
(not STRING )
I ran
find . -maxdepth 1 -type f -exec sed -i -E 's/{not\s([^\s}]+)\s}/(not \1 )/g' {} ;
It worked on some of the matches. When I run grep with the same pattern it shows more files that still have STRING. Ran find/sed again, same result.
You need to escape curly braces ({}), as they are regex meta-characters. Also \s is not POSIX sed, I would use the more portable [[:space:]].
Your code did not work on the example text for me (GNU/Linux). This does:
sed -E 's/\{not[[:space:]]+([^[:space:]}]+)[[:space:]]+\}/(not \1 )/g'
I also allowed for variable length whitespace directly after not and directly before } (using [[:space:]]+). You may or may not want that.
Also:
On MacOS sed I believe you need to supply a suffix argument to -i.
The trailing ; for find -exec must be quoted (\;) to avoid interpretation by the shell.
So the command would be:
find . -maxdepth 1 -type f -exec \
sed -E -i .TMP 's/\{not[[:space:]]+([^[:space:]}]+)[[:space:]]+\}/(not \1 )/g' {} \;
If .TMP conflicts with an existing file, choose a different suffix.

Find and rename files/folders using regex

I am trying to find a right regex for the filename that starts with I0[0-9][0-9]- eg: "I097-". I am not familiar with regex but using online, I came up with this [I][0][\d][\d][-], I am sure this is not the best regex pattern for the string I have, but I tested using online regex tools and it works. Now I want to use Linux 'find' to find all the files that match this regex and re-name the resulting files by replacing the matching string with nothing.
From:
I071-PTEN-7
./I071-PTEN-7/I071-PTEN-7.txt
To:
PTEN-7
./PTEN-7/PTEN-7.txt
command used:
find . -name "I0*" -type f -o -name "I0*" -type d -exec rename -n "s/[I][0][\d][\d][-]/''/" {} \;
But it doesn't seem to do anything, not sure what is going on. Any help to find the issue or solution would be greatly appreciated. Thanks.
Use -execdir option to get only filenames entries in find also there is no need to use character class around every character in your regex.
find . -name 'I0*' -execdir rename -n 's/^I0\d\d-//' {} \;
If rename isn't working then you may try this:
find . -type f -name 'I0*' -execdir bash -c 'mv "$1" "${1/I0[0-9][0-9]-/}"' - {} \; &&
find . -name 'I0*' -execdir bash -c 'mv "$1" "${1/I0[0-9][0-9]-/}"' - {} \;

Mac: replace text inside files in directory using regular expression

I need to replace all matches (regular expression) in directory from "*::" to be "\*::"
examples: (in multiple php files)
ID::
user::
process::
....
to be:
\ID::
\user::
\process::
I currently use,single commands
find . -type f -name '*.php' -exec sed -i '' s/ID::/\\\\ID::/ {} +
find . -type f -name '*.php' -exec sed -i '' s/user::/\\\\user::/ {} +
find . -type f -name '*.php' -exec sed -i '' s/process::/\\\\process::/ {} +
how to write regular expression to replace any "*::" => "\*::"
thanks,
You want to use "grouping" or "back-referencing" to refer back to a portion of the regular expression. This is done by surrounding the intended group with \( and \), and referring back to them with \1, \2, etc. In your case, the following expression should work:
$ echo -e "ID::\nuser::\nprocess::" | sed 's/\(.*\)::/\\\1::/g'
with the following output:
\ID::
\user::
\process::
I update "bebop" solution to meet my requirements, alot of thanks to him
solution:
sed -i -e 's/\([A-Za-z]*\)::/\\\1::/g' `grep -ril '::' *.php`

Linux Command: Find & Replace using regex not woking as expected

I am trying to replace a path in all php files using regex command, but it isn't working as expected!
I want to replace '/home/example/public_html with $_SERVER['DOCUMENT_ROOT'] . '
I am using the below command in ssh:
find /var/www/advertise/ -name '*.php' -type f -exec sed -i 's/\'\/home\/example\/public_html/\$\_SERVER\[\'DOCUMENT\_ROOT\'\]\ \.\ \'/g' {} \;
When i enter the command and hit return, > sign follows like:
>
>
>
.. so on as i keep hitting return to execute the command.
Where as below command works perfectly (for replacing home/example/public_html with var/www):
find /var/www/advertise/ -name '*.php' -type f -exec sed -i 's/home\/example\/public_html/var\/www/g' {} \;
You're messing up with the quotes.
Use a separator other than / so that you don't need to escape the /
You don't need to escape in the replacement
Since you have ' in the replacement, better use "s#..#..#" (i.e. double quotes). However, you'll need to escape the $ in the replacement to prevent the shell from trying to expand.
The following might work for you:
find /var/www/advertise/ -name '*.php' -type f -exec sed -i "s#'/home/example/public_html#\$_SERVER['DOCUMENT_ROOT'] . '#g" {} \;

batch process regular expression find and replace on folder and subfolders contents

I have a folder with subfolders that contain text documents (hundreds). The text documents all require a find and replace. The regular expression I am using to find the text is:
^([A-Z])[\r\n]+(\w+)\b
This is being replaced by:
$1$2
How can I batch process this find and replace on a folder with subfolders?
I'm using a mac (osx 10.6.8)
You could use sed for this as well:
cd /path/to/files # make sure you are in the right directory
find . -type f -exec sed -i.bak 's/^([A-Z])[\r\n]+(\w+)\b/$1$2/g' {} \;
Edit: I just realized that the above is a Textmate search/replace string. For sed you'll have to use:
find . -type f -exec sed -i.bak 's/^([A-Z])[\r\n]+(\w+)\b/\1\2/g' {} \;
This makes a backup of all files.
You could do this using find and perl:
find ./* -exec perl -p -i -e 's/^([A-Z])[\r\n]+(\w+)\b/$1$2/g' {} \;
Warning: untested :)