I have a folder with txt, html, jpg and png files.
Now I want to go over all textbased files (txt and html) and start a search and replace.
I did the following and it is working fine:
find ./ -iregex '.*\.\(txt\|html\)$' -exec sed -i 's/search/replace/g' {} +
But now I also want to log the files in which a replacement was executed.
Let's say I have five txt and/or html files in my folder. But in only two of them there will be something replaces because the string search
is in them.
So after the command I want to have a log file (for example myLog.log
) in which the two files are printed (full path).
But how can I achieve such a command?
The sed command does not have such a parameter/option.
You will have to search fist for search
pattern, then sed
, and log.
try (line break for readability)
find ./ -iregex '.*\.\(txt\|html\)$' \
-exec grep -q search {} \; \
-print \
-exec sed -i 's/search/replace/g' {} + \
> my-sed.log
where
search
in -exec grep -q search {} \;
is your search pattern. Note that you can't use +
who will match more that one file.-print
print matched file, and thus can logCollected from the Internet
Please contact [email protected] to delete if infringement.
Comments