Typically, when you get a >
in the next line after hitting, it means that one of your quotes isn't closed yet. I couldn't find that mistake in your regex. But you do not need to surround the path /var/www_data/somepath/
with single quotes. I assume there are no unusual characters in somepath
?
Anyways, I tested your regex with sed. \d\w
look like vim
syntax for me, that's why I translated it to ascii (which always works). Also, inside of []
you do not need to escape .
:
sed -r "s/'([A-Za-z0-9_-.]+)(@domain.com)'/'adsf'/g" test.dat
Indeed you can use sed
or perl
for your task. You don't necessarily need grep
to generate a file list, unless you have GB of data. Then presorting could result in a speed benefit.
To test your regex, you could do the following:
cd /var/www_data/somepath/
sed -r 's|pattern|replace-pattern|g' a_single_file.php
When you're satisfied with the result, just add the -ibak
(--in-place=bak
) argument and run it on all files
find . -type f -name '*.php' -o -name '*.ini' -o name '*.conf' -o -name '*.sh' \
-exec sed -r -ibak 's|pattern|replace-pattern|g' '{}' \;
The original files are being put into <orignalname.php>.bak
.
To answer your last question. For this job, grep
is the tool you want, you could run it on the .bak
files generated by sed above:
grep --recursive --include='*.bak' -E --files-with-matches 'pattern' . > files_fixed.txt
or, simply:
find . -type f -name '*.bak'
{ grep -m1 match; grep -c ''; } <file
That will work w/ GNU grep
and an lseek()
able infile. The first grep
will stop at 1 -m
atch, and the second will -c
ount every line remaining in input.
Without GNU grep
:
{ sed '/match/q'; grep -c ''; } <file
Of course, w/ grep
you can use any/all of its other options besides, and stopping at one match is not at all necessary.
Best Answer
You can pipe
grep
result tohead
.Note, that in order to ensure stopping after Nth match, you need to using
stdbuf
to make suregrep
don't buffer its output:As soon as
head
consumed 10 lines, it terminated andgrep
will receiveSIGPIPE
because it still output something to pipe whilehead
was gone.This assumed that no file names contain newline.