Text replacement in all the files in a directory - regex

In windows 2008, we have multiple files in a directory. The files are having paths as contents and those texts should be replaced with other string.Find example below:
The files are having the paths like:
File1:
C:\Apps\ etc\A1\X.exe should be replaced with C:\Apps\ exe\X.exe
File2:
C:\Apps\ etc\B1\Y.exe should be replaced with C:\Apps\ exe\Y.exe
I am trying to find a single command which will replace the bold lettered strings as mentioned above.
In case of normal strings I use the below command and it works:
perl -i.bak -pe "BEGIN{#ARGV = map glob, #ARGV} s/string1/string2/g" ./*.txt
But the current requirement seems to use regular expression for which I am not able to find a solution.

Just replace etc\ followed by anything up to a \ again with just exe:
's/etc\\[^\\]*\\/exe\\/g'

Related

Find and replace pattern in large number of files

I want to replace text in about 80.000 log files using a regex. I love the batch search and replace of VSCode. I was unable to do this with VSCode, because it did not seem to handle this amount of data well. Any suggestion how I could do this with VSCode? Are there suggestions for alternatives?
Instead of depending on a GUI based tool, it might be easier to for a CLI tool for this.
If you're using Linux, or willing to install any of the tools like sed and find if you're on Windows then it should be relatively simple.
You can use sed which is a command line tool on all (or at least most) distributions of Linux, and can be installed on Windows.
Usage (for this use case):
sed -i s/{pattern}/{replacement}/g {file}
Use sed to replace the matched pattern with a replacement, using the global modifier to match all results, and the file to do the replacement and overwrite.
To target all files in a directory you can do:
find -type f -name "*.log" exec sed -i s/{pattern}/{replacement}/g {};
Find items recursively starting from the current directory where it's type is file, and it has a name ending with .log. Then use sed to replace the pattern with the contents you want for each matched file.
You can find how to get tools like sed and find for Windows on the following question:
https://stackoverflow.com/a/127567/6277798

Grep with regex from file in bash script without inclusion of more folders

I have a file containing various paths such as:
/home/user/Desktop/Bash/file1.txt
/home/user/Desktop/Bash/file2.txt
/home/user/Desktop/Bash/file3.txt
/home/user/Desktop/Bash/moreFiles/anotherFile.txt
/home/user/Desktop/text/prettyFile.txt
And I receive a input from user that contains the directory, such as:
/home/user/Desktop/Bash/
And I usually save this expression into regex to find all the files in the directory by grep. However, if the folder has more folders, it includes them as well, but I want to only include the files in the directory that was entered by the user. My desired output is should be this:
/home/user/Desktop/Bash/file1.txt
/home/user/Desktop/Bash/file2.txt
/home/user/Desktop/Bash/file3.txt
But it keeps including
/home/user/Desktop/Bash/moreFiles/anotherFile.txt
which I don't want and I need to do it inside a bash script.
You can use this grep command to get just the files directly under given path skipping sub-directories:
s='/home/user/Desktop/Bash/'
grep -E "$s[^/]+/?$" file
/home/user/Desktop/Bash/file1.txt
/home/user/Desktop/Bash/file2.txt
/home/user/Desktop/Bash/file3.txt

Rename command with regular expression not working

I want to rename files using regex.
For example: replace pattern Mod[0-9][0-9] to Mod[0-9][0-9]_temp in files N_Mod10_m.bdf and N_Mod11_n.bdf using below command:
rename 's/\(.*Mod[0-9][0-9]\)\(.*\.bdf\)/$1_temp$2 *
but this is not working.
You need to use non-greedy pattern: (.*?). Also, add the missing quote '.
I guess, this is what you are looking for: rename 's/(.*?Mod[0-9][0-9])(.*?\.bdf)/$1_temp$2' *.
Have a look at Rename Multiple Files in a Shell Prompt and Renaming files to have lower case extensions with rename.
For CentOS, you can insert _temp into file name like this:
for i in *; do j=`echo $i | sed -r 's/(.*?Mod[0-9][0-9])(.*?\.bdf)/\1_temp\2/g'`; mv "$i" "$j"; done

Regex to add an extension to a directory full of files

I am new to regular expressions.
I have many irregularly numbered ascii files with no extension: g000554, g000556, g000558, g000561, g000563 ... g001979 etc
I would like to type a regex at the terminal (or in a short script) to add a .dat to all of these files.
So I would like to change them to become: g000554.dat, g000556.dat, g000558.dat, g000561.dat, g000563.dat ... g001979.dat etc
p.s. Sorry I should have provided more info: by terminal I meant a mac terminal and I cannot use the 'rename' command.
I think you're using a linux system. So i provide a bash solution. It works only if your files starts with g and there is no other files in that directory except the files you want to rename.
for i in g*; do mv "$i" "$i.dat"; done
The below would add .dat extension to all the files present in the current directory,
for i in *; do mv "$i" "$i.dat"; done

Regular expression search and replace in multiple files (bulk) - without opening files

I normally use Notepad++ to search and replace what I need (regex), however, I have to open all the files that I need, in order to replace what is needed to be replaced.. My question is how can I do that in bulk (multiple) files, in a folder, without opening any of the files? Is there a good freeware to do that with? or something like creating .bat or .pl file, and run it in the folder to execute the replace? If so, how can it be done?
Simple example:
<b>(\d+\. )</b>
to
\1
This regex removes the bold tag in numbers.
How can it be done for bulk files without using NP++ under Windows?
Use Notepad++'s own Find in files function, that you can find in the Find menu.
This can be done with this perl oneliner:
perl -pi.back -e 's#<b>(\d+\.\d+)</b>#$1#g;' file*
This will process all files that have their name beginning with file and save them before into fileX.back.