0

I want to run an egrep for several different patterns on a folder full of texts and output the results into corresponding folders. I'm also trying to make this neat and concise as this is a workflow I'll probably be using a lot.

So I have a folder full of texts called docs and I want to find two different regex patterns (regex1 and regex2) and output them into docs/regex1 and docs/regex2

I would guess that something like this would work

mkdir {docs/regex1,docs/regex2}
for file in docs/*
do
    egrep -o -f regex1 "$file" > regex1/"${file%.*}"_regex1.txt
    egrep -o -f regex2 "$file" > regex2/"${file%.*}"_regex2.txt
done

This gives me the error:

regex1/docs/001_regex1.txt: No such file or directory
regex2/docs/001_regex2.txt: No such file or directory

For each file. How can I set the output do go into docs/regex1 instead of trying to go to regex1/docs?

0

1 Answer 1

1

In your example script, you have:

egrep -o -f regex1 "$file" > regex1/"${file%.*}"_regex1.txt
egrep -o -f regex2 "$file" > regex2/"${file%.*}"_regex2.txt

But then you ask:

For each file. How can I set the output do go into docs/regex1 instead of trying to go to regex1/docs?

You're explicitly placing regex1 at the beginning of the path to which you're redirecting output. Simply fix this to match what you want, and strip the leading path off of $file:

egrep -o -f regex1 "$file" > docs/regex1/"$( basename "${file%.*}" )"_regex1.txt
egrep -o -f regex2 "$file" > docs/regex2/"$( basename "${file%.*}" )"_regex2.txt
0

You must log in to answer this question.

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