How can I perform an operation for each item listed by grep individually?
Background:
I use grep to list all files containing a certain pattern:
grep -l '<pattern>' directory/*.extension1
I want to delete all listed files but also all files having the same file name but a different extension: .extension2.
I tried using the pipe, but it seems to take the output of grep as a whole.
In find there is the -exec option, but grep has nothing like that.
If I understand your specification, you want:
This is essentially the same as what @triplee’s comment describes, except that it’s newline-safe.
What’s going on here?
grepwith--nullwill return output delimited with nulls instead of newline. Since file names can have newlines in them delimiting with newline makes it impossible to parse the output ofgrepsafely, but null is not a valid character in a file name and thus makes a nice delimiter.xargswill take a stream of newline-delimited items and execute a given command, passing as many of those items (one as each parameter) to a given command (or toechoif no command is given). Thus if you said:xargswould executeecho one 'two three' four. This is not safe for file names because, again, file names might contain embedded newlines.The
-0switch toxargschanges it from looking for a newline delimiter to a null delimiter. This makes it match the output we got fromgrep --nulland makes it safe for processing a list of file names.Normally
xargssimply appends the input to the end of a command. The-Iswitch toxargschanges this to substitution the specified replacement string with the input. To get the idea try this experiment:And note the difference from the earlier
printf | xargscommand.In the case of my solution the command I execute is
bash, to which I pass-c. The-cswitch causes bash to execute the commands in the following argument (and then terminate) instead of starting an interactive shell. The next block'rm "$1" "${1%.*}.extension2"'is the first argument to-cand is the script which will be executed bybash. Any arguments following the script argument to-care assigned as the arguments to the script. This, if I were to say:Then
Hello, worldwould be assigned to$0(the first argument to the script) and inside the script I couldechoit back.Since
$0is normally reserved for the script name I pass a dummy value (in this case--) as the first argument and, then, in place of the second argument I write{}, which is the replacement string I specified forxargs. This will be replaced byxargswith each file name parsed fromgrep‘s output beforebashis executed.The mini shell script might look complicated but it’s rather trivial. First, the entire script is single-quoted to prevent the calling shell from interpreting it. Inside the script I invoke
rmand pass it two file names to remove: the$1argument, which was the file name passed when the replacement string was substituted above, and${1%.*}.extension2. This latter is a parameter substitution on the$1variable. The important part is%.*which says%"Match from the end of the variable and remove the shortest string matching the pattern..*The pattern is a single period followed by anything.This effectively strips the extension, if any, from the file name. You can observe the effect yourself:
Since the extension has been stripped I concatenate the desired alternate extension
.extension2to the stripped file name to obtain the alternate file name.