1

I have a directory /foo/bar that contains many files, let's say a.file, b.file, c.file and so on. I am using grep to search for xyz pattern inside every file in that directory, but I want to send the result in new files such as a.new.file if the line was found in a.file, b.new.file if the line was found in b.file and so on.

My attempt: grep -ri "xyz" /foo/bar/* works well on screen, every line from the result starts with the path to the file that contains the pattern. Is this path stored in a variable that i can use to create the new files?

Edit:

Content of a.new.file should be all lines that match xyz from a.file

Content of b.new.file should be all lines that match xyz from b.file And so on.

3
for T in *.file
do
  grep -- xyz "$T" > "${T%.file}.new.file"
done

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.