opunsoars
opunsoars

Reputation: 115

Displaying the result of two grep commands in bash

I am trying to find the number of files in a directory with two different patterns in the filenames. I don't want the combined count, but display the combined result.

Command 1: find | grep ".coded" | wc -l | Output : 4533

Command 2: find | grep ".read" | wc -l | Output: 654

Output sought: 4533 | 654 in one line

Any suggestions? Thanks!

Upvotes: 1

Views: 65

Answers (2)

Benjamin W.
Benjamin W.

Reputation: 52102

With GNU find, you can use -printf to print whatever you want, for example a c for each file matching .coded and an "r" for each file matching .read, and then use awk to count how many of each you have:

find -type f \
    \( -name '*.coded*' -printf 'c\n' \) \
    -o \
    \( -name '*.read*' -printf 'r\n' \) \
    | awk '{ ++a[$0] } END{ printf "%d | %d\n", a["c"], a["r"] }'

By the way, your grep patterns match Xcoded and Yread, or really anything for your period; if it is a literal period, it has to be escaped, as in '\.coded' and '\.read'. Also, if your filenames contain linebreaks, your count is off.

Upvotes: 2

Jetchisel
Jetchisel

Reputation: 7781

With the bash shell using process substitution and pr

pr -mts' | ' <(find | grep "\.coded" | wc -l) <(find | grep "\.read" | wc -l)

Upvotes: 2

Related Questions