Help with egrep or grep command to meet multiple criteria


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with egrep or grep command to meet multiple criteria
# 1  
Old 02-25-2010
Help with egrep or grep command to meet multiple criteria

Hello,

I"m a newbie Smilie. 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.

Code:
egrep 'robot.*TLD | AA'  | wc -l

Are there any suggestion or recommendation?

Thanks so much in advance.

Filename output of testing.txt below:

Code:
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


Last edited by zxmaus; 02-26-2010 at 12:36 AM.. Reason: added code tags
# 2  
Old 02-25-2010
Not so clear, give us the final output you prefer.
# 3  
Old 02-25-2010
Code:
$ ROBOT=TLD
$ BARCODE=AA
$ sed -n "/robot:.*${ROBOT}/{N;/barcode:.*${BARCODE}/s/\n//p;}" file | wc -l
       2

Code:
$ ROBOT=TLD
$ BARCODE=BB
$ sed -n "/robot:.*${ROBOT}/{N;/barcode:.*${BARCODE}/s/\n//p;}" file | wc -l
       1

# 4  
Old 02-25-2010
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.

Code:
egrep "robot.*TLD.*AA"| wc -l


Last edited by zxmaus; 02-26-2010 at 12:37 AM.. Reason: added code tags
# 5  
Old 02-26-2010
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.

Code:
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

Code:
================================================================================
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)

# 6  
Old 02-26-2010
How will the code work if you change your input??

Code:
sed -n "/barcode:.*AA/{n;n;n;/robot type:.*TLD/p;}"  file | wc -l

# 7  
Old 02-26-2010
Thank you so much anbu23. It works great! I was able to understand the logic behind your script.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Insert content from file 1 to file 2 in specific criteria meet

Hi , I'm looking for some code that can copy and paste form file1 to file2 with 2 criterial meet. file1: test "sp-j1" test "sp-j2" test "sp-j3" test "sp-j4" file2: sub Pre_Shorts1 (Status_Code, Message$) global Status !if Message$ <> "" then print... (3 Replies)
Discussion started by: kttan
3 Replies

2. Shell Programming and Scripting

Only print specific xml values that meet two criteria in python

I have a large XML file that I want to parse, and only print one specific value if two values are met. This is the code so far: #!/usr/local/bin/python import xml.etree.ElementTree as ET tree = ET.parse('onedb-dhcp.xml') root = tree.getroot() # This successfully gets all... (1 Reply)
Discussion started by: brianjb
1 Replies

3. Shell Programming and Scripting

Multiple Grep command

Hi, I have an url.txt I need check them and grep some data. url.txt domain.com domain2.com domain3.com .....All sites urls have in source this patterns: "web=pattern1" "net++pattern2" "office**pattern3" I need this output: domain.com: pattern1,pattern2,pattern3 domain2.com:... (3 Replies)
Discussion started by: tara123
3 Replies

4. Shell Programming and Scripting

Finding the right file with multiple sort criteria

Hello, I have files in a directory with names like, ./f0/84.40_E1200_85.39_E1300_f0_r00_1300-ON-0.25_S7A_v4_47.19.1.out.txt ./f0/84.40_E1200_85.83_E1200_f0_r00_1200-ON-0.25_S7A_v4_47.19.1.out.txt ./f0/84.60_E1100_86.45_E1100_f0_r00_1100-ON-0.25_S7A_v4_47.19.1.out.txt... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

5. Shell Programming and Scripting

Globbling with multiple criteria (UNIX Shell)

I am new to UNIX Shell. I want to list the files names in the current directory that are not start with 'AB' and have at least two characters. For example, say I have those files in the current directory: AB, AC, AD, AE, B, C. After executing the command, AC, AD, AE will be listed on the screen. ... (6 Replies)
Discussion started by: Ray Sun
6 Replies

6. UNIX for Dummies Questions & Answers

Grep multiple strings in multiple files using single command

Hi, I will use below command for grep single string ("osuser" is search string) ex: find . -type f | xarg grep -il osuser but i have one more string "v$session" here i want to grep in which file these two strings are present. any help is appreciated, Thanks in advance. Gagan (2 Replies)
Discussion started by: gagan4599
2 Replies

7. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

8. UNIX for Dummies Questions & Answers

Difference between grep, egrep & grep -i

Hi All, Please i need to know the difference between grep, egrep & grep -i when used to serach through a file. My platform is SunOS 5.9 & i'm using the korn shell. Regards, - divroro12 - (2 Replies)
Discussion started by: divroro12
2 Replies

9. UNIX for Dummies Questions & Answers

How to use the "grep/egrep" command to search files.

Hi Team, I am new to this forum and also trying to learn Unix. I will highly appriciate your help if you can help me to get the right command . {{{ I use the command " today | egrep '(10:| 11: )' | grep ERROR " to grep all the files that has been error betweeen 10 to 11... (6 Replies)
Discussion started by: rkhanal
6 Replies

10. Shell Programming and Scripting

Searching for multiple criteria in log files?

I would like a simple shell script that will allow me to display to screen all unsuccessful su attempts in my sulog file, for the present date. I have been trying several different combinations of commands, but I can't quite get the syntax correct. The mess I have right now (don't laugh) is... (4 Replies)
Discussion started by: Relykk
4 Replies
Login or Register to Ask a Question