-2

I want to combine 2 different cat statements into a single cat statement

cat /dir1/file1.cfg | grep -q "linux"

cat /dir1/file2.cfg | grep -q "linux"

is there an OR operation that can be used here to do the grep -q "linux" on 2 files?

James Z
  • 12,209
  • 10
  • 24
  • 44
meallhour
  • 13,921
  • 21
  • 60
  • 117
  • 4
    In this case, why not just use: `cat /dir1/file*.cfg | grep -q "linux"`? – Joaquin Aug 08 '18 at 16:13
  • 1
    That is the original function of `cat`: To **conCATenate** and print files. In this case, you can use [brace expansion](https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html#Brace-Expansion) and do `cat /dir1/file{1..2}.cfg | grep -q "linux"` – dawg Aug 08 '18 at 16:53
  • 2
    As mentioned above -- if you *don't* want to concatenate multiple files, you **shouldn't be using `cat` at all**; it's much more efficient to give `grep` a direct handle on the files to search rather than forcing it to read from a FIFO connected to a separate process that is itself responsible for reading from the files. (With `grep`, actually, it may or may not be that significant -- but for `wc -c`, or `sort`, or `tail`, or other programs that can parallelize, seek, bisect, or otherwise use optimized implementations when given a real file handle the difference can be huge). – Charles Duffy Aug 08 '18 at 16:54

3 Answers3

2

What have you tried so far?

How about:

cat /dir1/file1.cfg /dir1/file2.cfg | grep -q "linux"

or with a subshell:

(cat /dir1/file1.cfg; cat /dir1/file2.cfg) | grep -q "linux"

or just:

grep -q "linux" /dir1/file1.cfg /dir1/file2.cfg

domen
  • 1,819
  • 12
  • 19
1

These all give equivalent results. The last would be how I'd write it.

% (cat f1; cat f2) | grep -q "linux"
% (cat f1 f2) | grep -q "linux"
% grep -q "linux" f1 f2
keithpjolley
  • 2,089
  • 1
  • 17
  • 20
0

There's really no need for cat at all - grep will happily take its input from multiple files; normally, you need to tell it not to prefix the file name to the output lines, but since you're using -q, even that isn't necessary:

grep -q "linux" /dir1/file[12].cfg

(I've used a shell glob to save writing the repeated parts of the filename there).

Toby Speight
  • 27,591
  • 48
  • 66
  • 103