Grep between 2 patterns in a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep between 2 patterns in a line
# 1  
Old 05-27-2010
Grep between 2 patterns in a line

Hi
I have a file containing 1000+ lines of netlist data.

I need to search for text between two key words on each line

for e.g my input file has

Code:
"ABC.ABC__312.deftr_0.X143.Xntys_0.\Xetabc__ABC_test_tz[0] .X1023" "ABC.ABC__312.asjartwtsj"
ABC.ABC__312.deftr0.X143.Xntys_0.\Xetabc__ABC_test_tz[0] .X1023 ABC.ABC__312.asjasXCsaj
"DEF.DEF__312.deftr_0.X143.Xntys_0.\Xetabc__DEF_test_tz[0] .X1023" "DEF.DEF__312.asjasdvsdj"
DEF.DEF__312.deftr0.X143.Xntys_0.\Xetabc__DEF_test_tz[0] .X1023 DEF.DEF__312.asjasdfseqwj

I want to grep the words between the first occurence of ABC and DEF and next occurence of ABC and DEF that occurs with a preceeding space.

i.e output would be

Code:
"ABC.ABC__312.deftr_0.X143.Xntys_0.\Xetabc__ABC_test_tz[0] .X1023" 
ABC.ABC__312.deftr0.X143.Xntys_0.\Xetabc__ABC_test_tz[0] .X1023 
"DEF.DEF__312.deftr_0.X143.Xntys_0.\Xetabc__DEF_test_tz[0] .X1023" 
DEF.DEF__312.deftr0.X143.Xntys_0.\Xetabc__DEF_test_tz[0] .X1023

I would highly appreciate you help,

Thanks
naveen
# 2  
Old 05-27-2010
check this:
Code:
awk -F\" '{print $2}' file



---------- Post updated at 09:43 PM ---------- Previous update was at 09:41 PM ----------

ok, you may not have quotes. this will work only for lines with quotes.

---------- Post updated at 09:46 PM ---------- Previous update was at 09:43 PM ----------

try simply:
Code:
awk '{print $1,$2}' file

# 3  
Old 05-27-2010
I need it to work for lines without quotes as well Smilie
# 4  
Old 05-27-2010
Try the later one.
# 5  
Old 05-27-2010
the
Code:
awk '{print $1, $2}' file

command doesnt take care of the spaces. As the 2nd key word is not constantly present in the 2nd column, if you carefully observe my code.

---------- Post updated at 08:26 AM ---------- Previous update was at 08:21 AM ----------

basically I need to grep for the data between "ABC and <space>"ABC,ABC and <space>ABC. Similarly for DEF
# 6  
Old 05-27-2010
I didn't get it.

This is what I am getting with the above command:
Code:
$ cat f
"ABC.ABC__312.deftr_0.X143.Xntys_0.\Xetabc__ABC_test_tz[0] .X1023" "ABC.ABC__312.asjartwtsj"
ABC.ABC__312.deftr0.X143.Xntys_0.\Xetabc__ABC_test_tz[0] .X1023 ABC.ABC__312.asjasXCsaj
"DEF.DEF__312.deftr_0.X143.Xntys_0.\Xetabc__DEF_test_tz[0] .X1023" "DEF.DEF__312.asjasdvsdj"
DEF.DEF__312.deftr0.X143.Xntys_0.\Xetabc__DEF_test_tz[0] .X1023 DEF.DEF__312.asjasdfseqwj
$ awk '{print $1,$2}' f
"ABC.ABC__312.deftr_0.X143.Xntys_0.\Xetabc__ABC_test_tz[0] .X1023"
ABC.ABC__312.deftr0.X143.Xntys_0.\Xetabc__ABC_test_tz[0] .X1023
"DEF.DEF__312.deftr_0.X143.Xntys_0.\Xetabc__DEF_test_tz[0] .X1023"
DEF.DEF__312.deftr0.X143.Xntys_0.\Xetabc__DEF_test_tz[0] .X1023
$

This is same as your expected output i think.
# 7  
Old 05-27-2010
Not sure what's wrong with using awk Smilie

However, with grep (and sed), something along the following lines should to, too:

Code:
[house@leonov] PT='DEF.' ; cat in.file | grep -o -E "$PT* $PT" | sed "s~$PT$~~"
DEF.DEF__312.deftr0.X143.Xntys_0.\Xetabc__DEF_test_tz[0] .X1023
[house@leonov] PT='"DEF.' ; cat in.file | grep -o -E "$PT* $PT" | sed "s~$PT$~~"
"DEF.DEF__312.deftr_0.X143.Xntys_0.\Xetabc__DEF_test_tz[0] .X1023"
[house@leonov] PT='ABC.' ; cat in.file | grep -o -E "$PT* $PT" | sed "s~$PT$~~"
ABC.ABC__312.deftr0.X143.Xntys_0.\Xetabc__ABC_test_tz[0] .X1023
[house@leonov] PT='"ABC.' ; cat in.file | grep -o -E "$PT* $PT" | sed "s~$PT$~~"
"ABC.ABC__312.deftr_0.X143.Xntys_0.\Xetabc__ABC_test_tz[0] .X1023"

(Depending on your actual requirements, you may want to rebuild this into a loop.)

Last edited by dr.house; 05-27-2010 at 02:09 PM.. Reason: Crap replaced by pseudo-crap ;-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep multiple patterns(file) and replace whole line

I am able to grep multiple patterns which stored in a files. However, how could we replace the whole line with either the pattern or new string? For example: pattern_file: *Info in the () is not part of the pattern file. They are the intended name to replace the whole line after the pattern... (5 Replies)
Discussion started by: wxboo
5 Replies

2. Shell Programming and Scripting

Find matched patterns and print them with other patterns not the whole line

Hi, I am trying to extract some patterns from a line. The input file is space delimited and i could not use column to get value after "IN" or "OUT" patterns as there could be multiple white spaces before the next digits that i need to print in the output file . I need to print 3 patterns in a... (3 Replies)
Discussion started by: redse171
3 Replies

3. Shell Programming and Scripting

Grep patterns

Hi Experts, I have a log file like this.I need to filter the Index name and elapsed time(only created ). 06:36:39 SQL> create index XYZ_F75 on XYZ 06:36:39 2 ("GRP_ID", "_ID") parallel 64 nologging 06:36:39 3 tablespace XARGS_IDX 06:36:39 4 ; Index created. Elapsed:... (2 Replies)
Discussion started by: navsan420
2 Replies

4. Shell Programming and Scripting

comment a line of the patterns is a the beginning of the line

I need to comment the lines starting with pattern "exclude" or "exclude=". If the work exclude comes at any other part, ignore it. Also, ignore, excludes, excluded etc. Ie only comment the line starting with exclude. File contents. exclude exclude= hi I am excluded excludes excludes= ... (9 Replies)
Discussion started by: anil510
9 Replies

5. UNIX for Dummies Questions & Answers

Grep - various patterns

I have a file with the following text: grep \$ grep \\$ grep \\\$ grep '\$' grep '\'$' grep \\ grep \\\\ grep "\$" grep '"$' grep "$" When I perform these same commands on this file, the result are never what I would expect them to be. Could someone please comment on the results and... (3 Replies)
Discussion started by: uran101
3 Replies

6. Shell Programming and Scripting

grep value between two patterns

Hi All, I've been trying solve this with a simple command but not having much luck. I have a file like this: Line 1: random_description 123/alert/high random_description2 356/alert/slow Line 2: random_description3 654/alert/medium Line 3: random_description4 234/alert/critical I'm... (7 Replies)
Discussion started by: joe19
7 Replies

7. Shell Programming and Scripting

grep for multiple patterns

I have a file with many rows. I want to grep for multiple patterns from the file. For eg: XX=123|YY=222|ZZ=566 AA=123|EE=222|GG=566 FF=123|RR=222|GG=566 DD=123|RR=222|GG=566 I want the lines which has both XX and ZZ. I know I can get it like this. grep XX file | grep YY But... (10 Replies)
Discussion started by: tene
10 Replies

8. Shell Programming and Scripting

Grep All lines between 2 different patterns

I need a simple script to get all lines between 2 Patterns, e.g. ............. ............. 114456723: testing Script Alpha Beta 114459234: testing Done ............. ............. It should give all the lines in between 114456723 and 114459234, including these as well. Any... (2 Replies)
Discussion started by: gurpreet470
2 Replies

9. Shell Programming and Scripting

grep patterns - File

Hi I have 3 patterns for example to be searched. These three patterns are available in file1. The patterns to be searched are in file2. I want to search the pattern of file1 to file2. Can any one help with example? Regards Dhana (1 Reply)
Discussion started by: dhanamurthy
1 Replies

10. Shell Programming and Scripting

Grep multiple patterns

Hi, Can we grep multiple patterns in UNIX. for example: cat /x/y/oratab | grep -i "pattern1|pattern2" .... etc I require the syntax for multiple patterns. | is not working as I explained in example. Malay (4 Replies)
Discussion started by: malaymaru
4 Replies
Login or Register to Ask a Question