0

Just looking for any other, perhaps better ways to do the following. Shell check is calling "SC2002 Useless cat" and every other way I have tried to mitigate has failed. It is working as desired, so I am inclined to just leave it alone - unless there are any other ideas out there.

#!/bin/bash
y=0
mapfile -O "$y" -t "ARRAY_NAME" < <(cat "${FILENAME}" | grep "pattern 1" | grep "pattern 2")

Works fine, but hits SC2002 for useless cat. So, I've tried to obvious:

mapfile -O "$y" -t "ARRAY_NAME" < <(grep -e "pattern 1" -e "pattern 2" "${FILENAME}")

No joy - results in "command grep not found"

Now, I can simply read the file, line-by-line into an array with mapfile, but I can't grep out only the lines I need - I can also do the same with read, which is probably better, but have the same grep problems.

So any ideas? Can I just ignore SC2002 in this case?

larsks
  • 277,717
  • 41
  • 399
  • 399

1 Answers1

2

Using 'cat filename' (single file) at the beginning of a pipeline can usually be eliminated by redirecting the standard input of the NEXT command in the pipe into the file.

mapfile -O "$y" -t "ARRAY_NAME" < <(grep "pattern 1" <"${FILENAME}"| grep "pattern 2")

Unless you care about every percentage of efficiency, and this line is the bottleneck for performance, safe to ignore, if you want, this message.

dash-o
  • 13,723
  • 1
  • 10
  • 37