Find and Replace string in all files recursive using grep and sed
As @Didier said, you can change your delimiter to something other than /
:
grep -rl $oldstring /path/to/folder | xargs sed -i s@$oldstring@$newstring@g
Using grep and sed to find and replace a string
You can use find
and -exec
directly into sed
rather than first locating oldstr
with grep
. It's maybe a bit less efficient, but that might not be important. This way, the sed
replacement is executed over all files listed by find
, but if oldstr
isn't there it obviously won't operate on it.
find /path -type f -exec sed -i 's/oldstr/newstr/g' {} \;
How can I grep/sed taking find/replace pairs from a file?
You can make a file with a list of sed
commands like this in a file called commands.sed
:
s|cat|cats|g
s|dog|dogs|g
s|person|people|g
and run it on some input with:
echo "House mouse cat dog person dog person" | sed -f commands.sed
and it will replace cat
with cats
, dog
with dogs
and person
with people
producing:
House mouse cats dogs people dogs people
So we want to turn your file with substitutions into a command file like that - using sed
! So, if your replacements file subs.txt
contains lines like this with the two words on each line separated by a TAB:
cat cats
dog dogs
person people
That would be:
sed -e 's/^/s|/' -e $'s/\t/|/' -e 's/$/|g/' subs.txt > commands.sed
and then you can apply it with:
sed -f commands.sed SomeFile
Rather than creating a file with the commands in, we can run a process substitution
like this to dynamically generate them, and do it all in one go with:
echo "House mouse cat dog person dog person" | sed -f <(sed -e 's/^/s|/' -e $'s/\t/|/' -e 's/$/|g/' subs.txt)
Replace string using grep and sed
sed will take multiple files as arguments, so this should work:
sed -i '/AB_/s//YZ_/g' *
(Note that -i
is non-standard)
Using grep and sed to replace one string with another
Provided you have a sed
which supports the -i
option,
sed -i 's/version: .*/version: 1.2.3/' file1 file2 file3 ...
You may want to tweak the regex wildcard; .*
matches through the end of the line, whereas [.0-9]*
matches the longest possible sequence of dots and digits. You might also want to permit for variations in surrounding whitespace ... But since this is probably among the top 10% FAQs on this site, go look for similar questions at this point.
To obtain the replacement string from file1 and apply it to file2, file3, etc, something like
new=$(sed -n 's/version: //p' file1)
# Use double quotes, not single, in order to expand $new
sed -i "s/version: [.0-9]*/version: $new/" file2 file3 ...
The first sed
invocation will only print lines on which "version: " was found and removed (replaced with an empty string). Presumably there will only be one such line in the file. Pipe the output to head -n 1
or uniq
or something, or find / create a more elaborate sed
script.
You normally use single quotes around literal strings, but since you don't want a literal $new
in the replacement, we use double quotes, which allow the shell to perform variable replacement (and a number of other substitutions we don't go into here) in the quoted string.
grep in file and replace string
Append this with GNU sed:
| sed 'N;s/\n/ /'
Output:
22-09-2015 15:36:10 sda 0,00 0,00 0,00 0,00 0,00 0,00 0,00 0,00 0,00 0,00 0,00 0,00 0,00
22-09-2015 15:36:12 sda 0,00 2,00 0,50 17,00 0,00 0,07 8,91 0,13 7,20 0,00 7,41 5,94 10,40
22-09-2015 15:36:14 sda 0,00 10,50 0,00 3,00 0,00 0,06 38,67 0,07 24,00 0,00 24,00 24,00 7,20
22-09-2015 15:36:16 sda 0,00 5,00 0,00 5,50 0,00 0,03 12,36 0,10 18,55 0,00 18,55 18,55 10,20
Related Topics
How to Source a Simple Bash Script
Implementation of Function Execve (Unistd.H)
Linux Kill Process Using Timeout in Milliseconds
Errors While Trying to Build Gdb for Arm
Case Statement in a While Loop, Shell Scripting
How to Strip Down My Yocto Linux
How to Determine from Where a Program Jumped to an Invalid Address - Without Single-Stepping
Sending Data on Af_Packet Socket
Inconsistent Systemd Startup of Freeswitch
Why Does Printf Still Work with Rax Lower Than The Number of Fp Args in Xmm Registers
Invalid Choice: 'Kernel_Add_Dts' in Yocto Build
How to Access Files in Hadoop Hdfs