Grep to find lines matching given patern in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep to find lines matching given patern in a file
# 1  
Old 05-02-2013
HP Grep to find lines matching given patern in a file

When I try with patern matching in a file with below code works

Code:
cat scj_drive_commands | egrep '/app/oracle/build_lib/pkg32|/app/oracle/build_lib/pkg33'


But when I have code with patern searching using of below does not work !

Code:
cat scj_drive_commands | egrep '/app/oracle/build_lib/pkg32*2012*|/app/oracle/build_lib/pkg33*2012*'

The sample file will look like;
Code:
# == Regular Build == Pre-processing
/app/oracle/build_lib/pkg320.0_20120927/ddl/scj_build_pre_rt.sh SID TEST_NUM 20120927 320.0 N N
# == Regular Build == Processing
/app/oracle/build_lib/pkg320.0_20120927/ddl/scj_build.sh SID TEST_NUM 20120927 320.0 N Y

# == Regular Build == Pre-processing - PROFIT!!!! - change goes into DEC MR pkg313
#/app/oracle/build_lib/pkg320.0_20121004_prof/ddl/scj_build_pre_rt.sh SID TEST_NUM 20121004_prof 320.0 N N

# == Regular Build == Pre-processing
/app/oracle/build_lib/pkg320.0_20121004/ddl/scj_build_pre_rt.sh SID TEST_NUM 20121004 320.0 N N
# == Regular Build == Processing
/app/oracle/build_lib/pkg320.0_20121004/ddl/scj_build.sh SID TEST_NUM 20121004 320.0 N Y

# == Regular Build == Pre-processing
/app/oracle/build_lib/pkg320.0_20121011/ddl/scj_build_pre_rt.sh SID TEST_NUM 20121011 320.0 N N
# == Regular Build == Processing
/app/oracle/build_lib/pkg330.0_20121011/ddl/scj_build.sh SID TEST_NUM 20121011 320.0 N Y

# == Regular Build == Pre-processing
/app/oracle/build_lib/pkg330.0_20121018/ddl/scj_build_pre_rt.sh SID TEST_NUM 20121018 320.0 N N
# == Regular Build == Processing
/app/oracle/build_lib/pkg330.0_20121018/ddl/scj_build.sh SID TEST_NUM 20121018 320.0 N Y

# 2  
Old 05-02-2013
Seems you are missing several dots in front of the stars, if you want to have any number of any type of character:
Code:
egrep '/app/oracle/build_lib/pkg32.*2012.*|/app/oracle/build_lib/pkg33.*2012.*' scj_drive_commands

The cat is not needed. grep takes a file as argument on it's own.
This User Gave Thanks to zaxxon For This Post:
# 3  
Old 05-02-2013
Quote:
Originally Posted by zaxxon
Seems you are missing several dots in front of the stars, if you want to have any number of any type of character:
Code:
egrep '/app/oracle/build_lib/pkg32.*2012.*|/app/oracle/build_lib/pkg33.*2012.*' scj_drive_commands

The cat is not needed. grep takes a file as argument on it's own.
It is HP-Unix Environment.


I do not understand, why should we put . dot before * asterisk.

Last edited by Siva SQL; 05-03-2013 at 02:37 AM.. Reason: Given command is working
# 4  
Old 05-02-2013
This should work:
Code:
$ grep '/app/oracle/build_lib/pkg3[23].*2012' scj_drive_commands
/app/oracle/build_lib/pkg320.0_20120927/ddl/scj_build_pre_rt.sh SID TEST_NUM 20120927 320.0 N N
/app/oracle/build_lib/pkg320.0_20120927/ddl/scj_build.sh SID TEST_NUM 20120927 320.0 N Y
#/app/oracle/build_lib/pkg320.0_20121004_prof/ddl/scj_build_pre_rt.sh SID TEST_NUM 20121004_prof 320.0 N N
/app/oracle/build_lib/pkg320.0_20121004/ddl/scj_build_pre_rt.sh SID TEST_NUM 20121004 320.0 N N
/app/oracle/build_lib/pkg320.0_20121004/ddl/scj_build.sh SID TEST_NUM 20121004 320.0 N Y
/app/oracle/build_lib/pkg320.0_20121011/ddl/scj_build_pre_rt.sh SID TEST_NUM 20121011 320.0 N N
/app/oracle/build_lib/pkg330.0_20121011/ddl/scj_build.sh SID TEST_NUM 20121011 320.0 N Y
/app/oracle/build_lib/pkg330.0_20121018/ddl/scj_build_pre_rt.sh SID TEST_NUM 20121018 320.0 N N
/app/oracle/build_lib/pkg330.0_20121018/ddl/scj_build.sh SID TEST_NUM 20121018 320.0 N Y

The .* means "zero or more of any character". It's the "filler" between 32 and 2012.
This User Gave Thanks to hanson44 For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print few lines before and after matching word is found suing grep?

Hi, here are few lines present in the logs. I want to grep on Error and print few lines before and after Error word is found line1 Line2 Line3 Error Line4 Line5 Line6 Line7 I want the output to be Line2 Line3 Error Line5 (1 Reply)
Discussion started by: arghadeep adity
1 Replies

2. UNIX for Dummies Questions & Answers

FIND matching pattern of lines in a file

I need to search for two patterns in a file and find number of matching lines. find . -type f | xargs grep "DROP TABLE" | wc -l find . -type f | xargs grep "DROP SYNONYM" | wc -l The above code works. However I am looking at finding a commnd that will simplify as on a singe command... (2 Replies)
Discussion started by: Siva SQL
2 Replies

3. UNIX for Dummies Questions & Answers

Grep to find matching patern and return unique values

Request: grep to find given matching patern and return unique values, eliminate the duplicate values I have to retrieve the unique folder on the below file contents like; /app/oracle/build_lib/pkg320.0_20120927 /app/oracle/build_lib/pkg320.0_20121004_prof... (5 Replies)
Discussion started by: Siva SQL
5 Replies

4. Shell Programming and Scripting

Grep the non-matching lines

Hi, I need to make a script to extract the number that are not in a file. Example: I have file-A that has 100000 (70000-799999) numbers. And a file-B with number that already are in the system. Now I need to know/get the numbers that are not in system. I was thinking something like this:... (5 Replies)
Discussion started by: AK47
5 Replies

5. Shell Programming and Scripting

Find the position of lines matching string

I have a file with the below format, GS*8***** ST*1******** A* B* E* RMR*123455(This is the unique number to locate this row) F* SE*1*** GE** GS*9***** ST*2 H* J* RMR*567889(This is the unique number to locate this row) L* SE* GE***** (16 Replies)
Discussion started by: Muthuraj K
16 Replies

6. Shell Programming and Scripting

Perl XML, find matching condition and grep lines and put the lines somewhere else

Hi, my xml files looks something like this <Instance Name="New York"> <Description></Description> <Instance Name="A"> <Description></Description> <PropertyValue Key="false" Name="Building A" /> </Instance> <Instance Name="B"> ... (4 Replies)
Discussion started by: tententen
4 Replies

7. Shell Programming and Scripting

Finding lines matching the Pattern and their previous lines in a file

Hi, I am trying to locate the occurences of certain pattern like 'Possible network disconnect' in a text file. I can get the actual lines matching the pttern using: grep -w 'Possible network disconnect' file_name. But I am more interested in getting the timing of these events which are... (7 Replies)
Discussion started by: sagarparadkar
7 Replies

8. Shell Programming and Scripting

AIX equivalent to GNU grep's -B and -A [print lines after or before matching lines]

Hi folks I am not allowed to install GNU grep on AIX. Here my code excerpt: grep_fatal () { /usr/sfw/bin/gegrep -B4 -A2 "FATAL|QUEUE|SIGHUP" } Howto the same on AIX based machine? from manual GNU grep ‘--after-context=num’ Print num lines of trailing context after... (4 Replies)
Discussion started by: slashdotweenie
4 Replies

9. Shell Programming and Scripting

find out line number of matching string using grep

Hi all, I want to display line number for matching string in a file. can anyone please help me. I used grep -n "ABC" file so it displays 6 ABC. But i only want to have line number,i don't want that it should prefix matching context with line number. Actually my original... (10 Replies)
Discussion started by: sarbjit
10 Replies

10. UNIX for Dummies Questions & Answers

Find matching lines between 2 files

How do I find matching lines between two files? (5 Replies)
Discussion started by: jojojmac5
5 Replies
Login or Register to Ask a Question