I have a text file, named "hosts.tbl":
BILL RED
VAL YELLOW
STEVE YELLOW
TOM ORANGE
BILLY RED
VALERIE BLUE
I have a second file, named "details.tbl" which has each name above, multiple times(among various other details on each line). I need to count how many times each name appears within "details.tbl", and end up with something like this:
BILL RED 8
VAL YELLOW 16
STEVE YELLOW 9
TOM ORANGE 1
BILLY RED 2
VALERIE BLUE 30
As you can see, a normal "grep" for 'BILL' will give me both "BILL" and "BILLY". Same for "VAL" and "VALERIE". However, within the "details.tbl" file, each occurrence of each name is followed by "-C". For example:
STEVE-C
STEVE-C
BILL-C
BILLY-C
I have tried:
awk {'print $1 " " $2 " "'} hosts.tbl|grep -c $1"-C" details.tbl
awk {'print $1 " " $2 " "'grep -c $1"-C" details.tbl} hosts.tbl
...and various other permutations of similar syntax, above...all dismal failures. Clearly, I am a neophyte when it comes to shell commands in particular, and UNIX in general. What am I missing, here? I cannot find anything in the man pages about how to concatenate search criteria within grep, or how to pass only specific fields from awk along to grep.
Assuming the applicable portion of the details.tbl file looks like this:
BILL-C
VAL-C
STEVE-C
TOM-C
BILLY-C
VALERIE-C
BILL-C
VAL-C
STEVE-C
TOM-C
BILLY-C
VALERIE-C
The output should look like this:
BILL RED 2
VAL YELLOW 2
STEVE YELLOW 2
TOM ORANGE 2
BILLY RED 2
VALERIE BLUE 2