1

I am using the following command (on Solaris) to remove the protocol from a bunch of files.

find .  -name "*.txt"  -exec perl -pi  -e 's/http//g' '{}' \;

Is there a way to have a verbose output so I can get a report of which files have been edited?

Thanks, Andy

1
  • Can you just add -print to the find command? Nov 6, 2015 at 16:28

2 Answers 2

3

You could get a count of substitutions per file with:

find . -type f -exec perl -pi -e '
    $count{$ARGV} += s/http//g;
    END {for (keys %count) {print "$_: $count{$_}\n" if $count{$_}}}' {} +

Note that in that and in your original solution, perl will rewrite the files regardless of whether it does substitution in them or not.

Don't use ; to terminate the -exec command. perl can handle several files at a time, no need to call one perl per file.

If you only want that output without actually doing the substitutions, just replace -pi with -n in the command above.

You could also do:

find . -type f -exec grep -c http /dev/null {} +

To get a count of lines containing http (not necessarily the same as the number of http occurrences).

2
  • Thanks for that Stephane. The only thing is, I was hoping to get the report first (without substitution) by dropping the -i, but that just lists all lines in all files. Is there an easy way to get the report of what will be substituted? This is going to be run on a LOT of files so I want to be sure I am doing the right thing. Nov 6, 2015 at 16:50
  • @user2609641, replace -pi with -n. See edit. Nov 6, 2015 at 16:59
1

You can get a report of what files are sent to perl by using both -print and -exec:

find .  -name "*.txt" -print -exec perl -pi  -e 's/http//g' '{}' \;

If you want to find out which files were modified, then something like

find .  -name "*.txt" \
    -exec perl -i.bak -pe 's/http//g' '{}' \; \
    -exec sh -c 'cmp -s "$1" "$1.bak" && echo "$1" || rm "$1.bak"' _ '{}' \;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .