LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-02-2018, 03:41 AM   #1
kk2018
LQ Newbie
 
Registered: Nov 2018
Posts: 1

Rep: Reputation: Disabled
Question Searching a file for multiple patterns recursively with IF condition


Lets say we have three strings Str1, Str2 and Str3 among many other strings in a huge file.

Str1, Str2 and Str3 can occur multiple times in the file.

But Str2 and Str3 occurs only after Str1. The pattern would be something like

Str1
Str2
Str3

Str1
Str2
Str3

So, I need to search this file for Str1, whenever this occurs, I need to search for Str2 and Str3 until I find Str1 then again I need to search for Str2 and Str3 and this repeats till the end of the file.

I have used SED, AWK and GREP for simple search. I have already spent some time and read about RE and understood a bit of it but this seems to be complex

Last edited by kk2018; 11-02-2018 at 03:42 AM.
 
Old 11-02-2018, 03:55 AM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
The description is not quite clear. Does Str2 come right after Str1 or anywhere after Str1? Are you searching for entire lines or are the strings embedded in text lines? Is there anything to do once you have found a str1-str2-str3 pattern?

Also, can you share what you have done so far? Perhaps the most promising attempt at a solution.
 
Old 11-02-2018, 04:01 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Well, something like this might be what you want
Code:
# test file t.t content
trx
str1
str2
str3
strc
strv
str1
str2
str3
strf

# now the code & o/p
sed -nE '/str1/,/str3/p' t.t

str1
str2
str3
str1
str2
str3
but as above, there are some ambiguities in your spec..
 
Old 11-02-2018, 04:02 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
in awk you can do something like this:
Code:
awk '
/Str1/ { s1=1 }   # set variable s1 to 1 if Str1 found.
/Str2/ { if ( s1 == 1 ) s2=1 }   # similar to Str2
...
But this is just an idea, you need to specify what you need better.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Searching for multiple patterns with grep dznunn Programming 2 06-26-2012 09:52 AM
searching multiple patterns through Vi editor barunparichha Linux - Software 10 05-12-2009 08:39 PM
If condition for checking patterns in multiple files rattlesnakejoe Programming 5 05-07-2009 02:52 AM
Searching patterns from file MichaelVaughn Programming 1 04-06-2004 11:18 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration