I'm not very experienced in shell scripting, but I'm trying to understand how to grep for a pattern and for each file where there is a match write a file to disk that contains the matched line from grep. For example:

$ grep -E "MY PATTERN" myfile{1,3}

Then write out new files that contain the matching lines:


Is this possible? How can I do this? I'm looking for an answer is bash or zsh.


Bonsi has the right idea, but not the right approach (what should one do with filenames containing spaces or other whitespace characters, for example). Here is a way of doing it in bash:

for file in myfile{1,3}; do
    grep -E "MY PATTERN" < "$file" > "matches-$file"

If you did need to store the files you should account properly for word splitting, like so:

files=( myfile{1,3} )
for file in "${files[@]}"; do
    grep -E "MY PATTERN" < "$file" > "matches-$file"
  • 1
    I fixed that flaw in his approach
    – jordanm
    Nov 9 '12 at 15:46
  • Note that a difference with @Guru's approach is that if "MY PATTERN" is not to be found in myfile2 or if myfile2 doesn't exist or is not readable, then a matches-myfile2 will still be created (empty). Nov 10 '12 at 20:17

One way using awk:

awk '/MY PATTERN/{print > "matches-"FILENAME;}' myfile{1,3}

i don't think that this is possible however it is possible to build a small script for that

#! /bin/bash
files=(file1 file2 file3)
for i in "${files[@]}"; do grep -E "MY PATTERN" < "$i" > matches-"$i"; done

You can do it like this with GNU find and GNU parallel:

find . -type f -print0 | parallel -0 grep '"MY PATTERN"' '{} > {//}/matches.{/}'

You can make grep output the filename with -H, and then use awk to write to it.

$ grep -H regexp files*  | awk -F : '{ file="matches-" $1; sub("^[^:]+:","",$0); print $0 > file; }'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.