Struggling to combine two Greps statements


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Struggling to combine two Greps statements
# 1  
Old 02-17-2011
Struggling to combine two Greps statements

Greetings!

I have been tasked to create a report off files we receive from our hardware suppliers. I need to grep these files for two fields 'Test_Version' and 'Model-Manufacturer' ; for each field, I need to capture their corresponding values.

When running each statement separately, I get the results I want:

1) for 'Test_Version', it was straightforward:

Code:
    find . -name "*.VER" -exec grep 'Test_Version=' '{}' ';' -print; 

    ./(FILE_NAME).VER
    Test_Version=2.6.3
    ./(FILE_NAME).VER
    Test_Version=2.4.7

2) for 'Model-Manufacturer', , it was a bit tricky since what I need is across multiple lines. I solved this issue by using Perl Regex option -P.

Code:
    find . -name "*.VER" -exec grep -P 'Model-Manufacturer:.\n.' '{}' ';' -print

    ./(FILE_NAME).VER
    --> Model-Manufacturer:
    D12-100
    ./(FILE_NAME).VER
    --> Model-Manufacturer:
    H21-100

Ideally, I would like to create a simple report that looks like this

Code:
    (FILE_NAME)
    Test_Version=2.6.3
    Model-Manufacturer:    D12-100
    (FILE_NAME)
    Test_Version=2.4.7
    Model-Manufacturer:    H21-100


My attempt to combine both greps is not working i.e nothing is found:

Code:
    find . -name "*.VER" -exec grep -P 'Test_Version=.Model-Manufacturer:.\n.' '{}' ';' -print

How can I grep to search for both fields and produce the output I want?
# 2  
Old 02-17-2011
grep has an OR feature:
Code:
grep -E 'pat1|pat2'

so you could use a single grep for your search. Alternatively:
Code:
egrep 'pat1|pat2'

Andrew
# 3  
Old 02-17-2011
Quote:
Originally Posted by apmcd47
grep has an OR feature:
Code:
grep -E 'pat1|pat2'

so you could use a single grep for your search. Alternatively:
Code:
egrep 'pat1|pat2'

Andrew
Actually, I must find both expressions. My apologies if I wasn't clear.

Does grep have an AND feature?
# 4  
Old 02-17-2011
Quote:
Originally Posted by alan

Does grep have an AND feature?
Not really I think I'd go for an awk solution, but I don't know awk.
Code:
find. -name "*.VER" -print0 | xargs -0 grep -E 'pat1|pat2' | awk 'something'

The input to awk will be something like:
Code:
./file1.VER:Test_Version=2.6.3
./file1.VER:Model-Manufacturer:    D12-100

so the awk script will split the lines into( name, line) and print the name and lines when it counts two or more instances of the same file name.

So now all we need is for someone to come up with the correct incantation for awk...

If no-one comes up with an awk solution I'll attempt one in perl.

Andrew
# 5  
Old 02-17-2011
Quote:
Originally Posted by apmcd47
Not really I think I'd go for an awk solution, but I don't know awk.
Code:
find. -name "*.VER" -print0 | xargs -0 grep -E 'pat1|pat2' | awk 'something'

The input to awk will be something like:
Code:
./file1.VER:Test_Version=2.6.3
./file1.VER:Model-Manufacturer:    D12-100

so the awk script will split the lines into( name, line) and print the name and lines when it counts two or more instances of the same file name.

So now all we need is for someone to come up with the correct incantation for awk...

If no-one comes up with an awk solution I'll attempt one in perl.

Andrew
Go this one working with decent results

Code:
find . -name "*.VER" -exec egrep -A 1 'Test_Version=|Model-Manufacturer:' {} ';'

I need to figure out how to wrap output lines up...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Combining multiple greps

I'm trying to learn about regular expressions. Let's say I want to list all the files in /usr/bin beginning with "p", ending with "x", and containing an "a". I know this works:ls | grep ^p | grep x$ | grep abut I'm thinking there must be a way to do it without typing grep three times. Some of my... (9 Replies)
Discussion started by: Xubuntu56
9 Replies

2. Shell Programming and Scripting

Perl combine multiple map statements

I have a file like file. file.TODAY.THISYEAR file.TODAY.LASTYEARI want to substitute the words in caps with their actual values so that output should look like file.140805 file.140805.2014 file.140805.2013For this I am reading the file line bye line in an array and using multiple map... (1 Reply)
Discussion started by: sam05121988
1 Replies

3. Shell Programming and Scripting

Combine 4 awk pattern count statements into 1

Hello fellow awkers, I am trying to combine the following awk statements into 1 so that the results are more accurate: awk '/\=\+/ { count++ } END { print count}' filename awk '/\=\?/ { count++ } END { print count}' filename awk '/\=\-/ { count++ } END { print count}' filename awk... (8 Replies)
Discussion started by: ux4me
8 Replies

4. UNIX for Dummies Questions & Answers

Combine two awk statements into one

Hi, I have the following two awk statements which I'd like to consolidate into one by piping the output from the first into the second awk statement (rather than having to write kat.txt out to a file and then reading back in). awk 'BEGIN {FS=OFS=" "} {printf("%s ", $2);for (x=7; x<=10;... (3 Replies)
Discussion started by: kasan0
3 Replies

5. UNIX for Dummies Questions & Answers

combine two greps

Hi, Syntax 1: /usr/bin/svcs -a |grep apache22 | wc -l 2 Syntax 2: ps -ef | grep wlsdmin | grep -v "grep" | wc -l 3 I need to combine both the above syntax and get 2+3 i.e. 5 as the resultant output. What's the simplest way to achieve this ? (7 Replies)
Discussion started by: shifahim
7 Replies

6. Shell Programming and Scripting

simplify/combine if statements would be nice

below is something i inherited: if && && ; then HOST_SELECT="-m quadcore" fi if && && ; then HOST_SELECT="-m quadcore" fi if && && ; then HOST_SELECT="-m octocore1" fibelow is what i changed it to: if && && ; then HOST_SELECT="-m quadcore"... (2 Replies)
Discussion started by: crimso
2 Replies

7. Shell Programming and Scripting

Combine awk statements

I have an awk statement that works but I am calling awk twice and I know there has to be a way to combine the two statements into one. The purpose is to pull out just the ip address from loopback1. cat config.txt | nawk 'BEGIN {FS="\n"}{RS="!"}{if ( $0 ~ "interface loopback1" ) print$4}' | nawk... (5 Replies)
Discussion started by: numele
5 Replies

8. Shell Programming and Scripting

Struggling with AWK

I know this is an easy problem, but I've been struggling with it all morning and finally admit I need help. I have two files that I need to compare. After doing this the easy way (grep -F) I realize that there are other fields in file2 that match field1 in file1, so I turned to awk. I want to... (7 Replies)
Discussion started by: moldoverb
7 Replies

9. Shell Programming and Scripting

combine two grep statements

Hi I am wondering is it possible to combine two greps together I have two greps. grep "^,, *\." file (grep the line which has a '.' in the third column) grep "=" file (grep the line which has = anywhere) How to put them together so that if the content of the file that match either... (1 Reply)
Discussion started by: tiger66
1 Replies

10. UNIX for Dummies Questions & Answers

How to combine case statements

Hi, I need to change military time to regular time. I know to use case to indicate whether a.m. or p.m. as follows: case "$hour" in 0? | 1 ) echo a.m.;; 1 ) echo p.m.;; * ) echo p.m.;; esac My question is how do I add the hour and minute... (2 Replies)
Discussion started by: karp3158
2 Replies
Login or Register to Ask a Question