0

I have some files in a folder. By running the following command I can create a list with the filtered filenames ( test1, test2,test3) and pass it using xargs to a grep filtering command filter file in the command contains a few values to be filtered out.

ls -Art1 | grep test | xargs -L1 grep -vf filter > ouput

However when run this command output file contain the filtered result of test1, test2 and test3. All in one.

I would like to have separate file for each test1 > test1_output, test2 >test2_output,... so taking the xargs value and just add an extra string to it "_output" to generate an output filename

peti27
  • 51
  • It's not recommendable parse ls. Check this – Edgar Magallon Oct 20 '22 at 22:46
  • 2
    The link. was useful, however in my case not really relevant. I am well aware of the "space in the name" issue and I always avoid it. These scripts are for personal use only so no big risk of faulty output. Thanks for the comment. – peti27 Oct 21 '22 at 04:29
  • You're welcome! And that's correct. While you don't have any problems with special filenames you can use lswithout any problem although it's useful to know about that. – Edgar Magallon Oct 21 '22 at 20:36

1 Answers1

1

There's no need to pipe the output of ls to search for a name match. Just use a shell glob. This also allows you to define an output file for each filter attempt

for file in *test*
do
    [ -f "./$file" ] && grep -vf filter "./$file" >"${file}_output"
done

Technically this is potentially a slightly different set of file matches to your ls -A as your code considers dot files whereas mine does not. Fixable if relevant.

In your comment you mention you are performing two actions on each file. If I have understood you correctly, then for such a situation you can modify the code like this:

for file in *test*
do
    if [ -f "./$file" ]
    then
        grep -f filter "./$file" >"${file}_delta"
        grep -vf filter "./$file" >"${file}_output"
    fi
done
Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • It is working as expected! Thanks! – peti27 Oct 21 '22 at 00:23
  • 1
    I played a bit more with the solution and actually love it even more. I added additional command to the script so now not only generates the output but also creates a delta between them. The "_delta" output is actually the same as executing the same line without the grep exclusion ("v") option. – peti27 Oct 21 '22 at 04:23
  • @peti27 I've given you an option to address the _delta you mention – Chris Davies Oct 21 '22 at 16:41