Search a string inside a pattern matched block of a file


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Search a string inside a pattern matched block of a file
# 1  
Old 04-21-2017
Search a string inside a pattern matched block of a file

How to grep for searching a string within a begin and end pattern of a file.

Sent from my Redmi 3S using Tapatalk
# 2  
Old 04-21-2017
Code:
awk '/STARTREGEX/ { P=1 } ; /ENDREGEX/ { P=0 } ; P && /LINEREGEX/' datafile

If that doesn't work we will need a LOT more details.
# 3  
Old 04-24-2017
Thank you for the help.

However, I wanted to fetch the first line after the 'STARTREGEX' as well, as each pattern block name(1st line after the pattern) containing the matched string is also required.
I missed to mention the above prior !

E.g.:

Code:
START 
Block1
..
..
string
..
END 
START 
Block2
..
..
END 
START 
Block3
..
string
..
END

So, here I wanted the output like:
Code:
Block1 string
Block3 string

Awaiting your suggestions..!
Moderator's Comments:
Mod Comment Please use CODE tags (as required by forum rules) when displaying sample input, sample output, and code segments.

Last edited by Don Cragun; 04-24-2017 at 03:30 AM.. Reason: Add CODE tags.
# 4  
Old 04-24-2017
Quote:
Originally Posted by Baishali
Thank you for the help.

However, I wanted to fetch the first line after the 'STARTREGEX' as well, as each pattern block name(1st line after the pattern) containing the matched string is also required.
I missed to mention the above prior !

E.g.:

Code:
START 
Block1
..
..
string
..
END 
START 
Block2
..
..
END 
START 
Block3
..
string
..
END

So, here I wanted the output like:
Code:
Block1 string
Block3 string

Awaiting your suggestions..!
Moderator's Comments:
Mod Comment Please use CODE tags (as required by forum rules) when displaying sample input, sample output, and code segments.
Given the code that Corona688 suggested, can you show us how you might be able to modify that suggestion to get the output you want.
# 5  
Old 04-24-2017
I tried the below code :

Code:
awk '/STARTREGEX/{P=1;getline;print;} ; /ENDREGEX/ { P=0 } ; P && /LINEREGEX/{ print }' datafile

This however prints all the block names along with the lines where the string is matched.

Code:
Block1 string
Block2
Block3 string

# 6  
Old 04-24-2017
How about
Code:
awk '/^Block/ {BL = $0} /START/,/END/ {if (/string/) print BL, $0}' file
Block1 string
Block3 string

# 7  
Old 04-24-2017
Quote:
Originally Posted by RudiC
How about
Code:
awk '/^Block/ {BL = $0} /START/,/END/ {if (/string/) print BL, $0}' file
Block1 string
Block3 string

---------- Post updated at 11:58 AM ---------- Previous update was at 11:36 AM ----------

Thank you RudiC for the alternate solution,
However the word Block was to illustrate an example, the file actually has different block names and only identifier is it is the next line after the STARTREGEX matched.

Code:
START
Block 1
..
string
..
END
START
Group 2
..
END
START
BLK 3
..
string
..
END

Result needed:
Block 1 string
BLK 3 string

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can I use find command to search string/pattern in a file recursively?

Hi, How can I use find command to search string/pattern in a file recursively? What I tried: find . -type f -exec cat {} | grep "make" \; Output: grep: find: ;: No such file or directory missing argument to `-exec' And this: find . -type f -exec cat {} \; -exec grep "make" {} \;... (12 Replies)
Discussion started by: cola
12 Replies

2. Shell Programming and Scripting

Search for Pattern as output between the matched String

Hello, I have a file which has the below contents : VG_name LV_name LV_size in MB LV_option LV_mountpoint owner group y testdg rahul2lv 10 "-A y -L" /home/abc2 ... (6 Replies)
Discussion started by: rahul2662
6 Replies

3. Shell Programming and Scripting

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

4. Shell Programming and Scripting

Perl - use search keywords from array and search a file and print 3rd field when matched

Hi , I have been trying to write a perl script to do this job. But i am not able to achieve the desired result. Below is my code. my $current_value=12345; my @users=("bob","ben","tom","harry"); open DBLIST,"<","/var/tmp/DBinfo"; my @input = <DBLIST>; foreach (@users) { my... (11 Replies)
Discussion started by: chidori
11 Replies

5. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

6. Shell Programming and Scripting

Awk search for string pattern in delimited file

I've got a semicolon delimited file. I would like to search for fields that match a pattern, and not hardcoded eg "mth". *th=something If the delimited field fulfills this condition, eg. mth=value I would like to print out both key and value for some number comparison. eg. if value > "12"... (5 Replies)
Discussion started by: alienated
5 Replies

7. Shell Programming and Scripting

search for the matched pattern by tracing back from the line

Hi, I want to grep the line which has 'data11'.then from that line, i need to trace back and find out the immediate line which has the same timestamp of that grepped line. for eg: log file: ----------- Process - data Process - datavalue - 2345 Process - data Process - data Process... (9 Replies)
Discussion started by: Sharmila_P
9 Replies

8. Shell Programming and Scripting

Search Mulitiple String pattern in a file

Hi, I need to search for a multiple string pattern(5 key words) in a file(similar to a flat file) ,and i need to store the output in a another file . In that file we may have mutiple occurrences of the key words.and i need only the unique. kindly help out. Thanks, Mohana Krishnan (2 Replies)
Discussion started by: krishnan_6015@y
2 Replies

9. UNIX for Dummies Questions & Answers

Search and Parse string from inside a File

Hello, I barely know the basics, but I am very determined to learn. I want to parse a few characters from each row, use that string to search another file and display the line number where I found the value in the file. I don't know if this can all be done on the command line, so I am creating a... (2 Replies)
Discussion started by: SSims
2 Replies

10. Shell Programming and Scripting

How to search a pattern inside a zipped file ie (.gz file) with out unzipping it

How to search a pattern inside a zipped file ie (.gz file) with out unzipping it? using grep command.. Bit urgent.. pls..help me (2 Replies)
Discussion started by: senraj01
2 Replies
Login or Register to Ask a Question