MinBee
1
Hello,
I"m a newbie :). I hope I can learn from the scripting expert.
I'm trying to use egrep and grep commands to get the total count by meeting both criteria. So far, I haven't been able to do it.
if robot = TLD and barcode = AA, then final count should be 2
if robot = TLD and barcode = BB, then final count should be 1
I tried the following command below, but it picks up everything with TLD and AA. I would like it to pick up only if both matches.
egrep 'robot.*TLD | AA' | wc -l
Are there any suggestion or recommendation?
Thanks so much in advance.
Filename output of testing.txt below:
media: AA12345
robot: TLD library
barcode: AA12345
volume: TESTING
=================
media: AA56789
robot: NONE library
barcode: AA56789
volume: TESTING
=================
media: BB88888
robot: TLD library
barcode: BB88888
volume: TESTING
=================
media: AA44444
robot: TLD library
barcode: AA44444
volume: TESTING
=================
media: BB77777
robot: NONE library
barcode: BB77777
volume: TESTING
rdcwayx
2
Not so clear, give us the final output you prefer.
anbu23
3
$ ROBOT=TLD
$ BARCODE=AA
$ sed -n "/robot:.*${ROBOT}/{N;/barcode:.*${BARCODE}/s/\n//p;}" file | wc -l
2
$ ROBOT=TLD
$ BARCODE=BB
$ sed -n "/robot:.*${ROBOT}/{N;/barcode:.*${BARCODE}/s/\n//p;}" file | wc -l
1
I was unable to understood your full requirement.But,if you want to match both TLD and AA in same line.Then,you use the following.
egrep "robot.*TLD.*AA"| wc -l
MinBee
5
Thanks rdcwayx, anbu23, and vivekraj for your response.
The output is very large, but I will trim it down to 6 records in the file.
I attempted to use anbu23 code, but it kept returning with 0 count.
sed -n "/robot type:.*TLD/{N;/barcode:.*AA/s/\n//p;}" testing.txt | wc -l
I hope I can make this one more clear.
There are 6 records in the testing.txt file. For each record, I would like a count if it fits the criteria with barcode of AA* and robot type of TLD*.
Based on the 6 records, I should get a total count of 4 that fits the barcode of AA* and robot type of TLD*.
barcode AA0379, AA0400, AA0500, AA0700 all fit the criteria.
Thanks in advance.
Filename: testing.txt
================================================================================
media ID: AA0379
media type: 1/2" cartridge tape 3 (24)
barcode: AA0379
media description: ---
volume pool: ScratchPool (7)
robot type: TLD - Tape Library DLT (8)
================================================================================
media ID: BB0234
media type: 1/2" cartridge tape 3 (24)
barcode: BB0234
media description: ---
volume pool: ScratchPool (7)
robot type: NONE - Not Robotic (0)
================================================================================
media ID: AA0400
media type: 1/2" cartridge tape 3 (24)
barcode: AA0400
media description: ---
volume pool: ScratchPool (7)
robot type: TLD - Tape Library DLT (8)
================================================================================
media ID: AA0500
media type: 1/2" cartridge tape 3 (24)
barcode: AA0500
media description: ---
volume pool: ScratchPool (7)
robot type: TLD - Tape Library DLT (8)
================================================================================
media ID: AA0600
media type: 1/2" cartridge tape 3 (24)
barcode: AA0600
media description: ---
volume pool: ScratchPool (7)
robot type: NONE - Not Robotic (0)
================================================================================
media ID: AA0700
media type: 1/2" cartridge tape 3 (24)
barcode: AA0700
media description: ---
volume pool: ScratchPool (7)
robot type: TLD - Tape Library DLT (8)
anbu23
6
How will the code work if you change your input??
sed -n "/barcode:.*AA/{n;n;n;/robot type:.*TLD/p;}" file | wc -l
MinBee
7
Thank you so much anbu23. It works great! I was able to understand the logic behind your script.
rdcwayx
8
Only awk:
awk 'BEGIN{RS="=+";FS="\n"} /barcode.*AA/&&/robot.*TLD/ { sum++ } END {print sum}' file
fubaya
9
Only grep... and grep. This will fail if the lines don't appear exactly as they did in your example, as will sed. The awk line doesn't appear to.
# grep -B 3 TLD testfile | grep -c AA
4
#
It greps TLD and looks up 3 lines and counts how many "AA"s.
MinBee
10
Thanks rdcwayx and fubaya for your suggestion. The sed command works beautiful, but I'm curious to test your solution as well.
Again, thank you.