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?


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.


1 Answer 1

for T in *.file
  grep -- xyz "$T" > "${T%.file}.new.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.