Skip to main content

grep search pattern in multiple files and output to different files [Resolved]

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.


Question Credit: Alex
Question Reference
Asked August 22, 2018
Tags: , grep
Posted Under: Unix Linux
13 views
1 Answers

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

credit: Gerard H. Pille
Answered August 22, 2018
Your Answer
D:\Adnan\Candoerz\CandoProject\vQA