I want to search a file to check if there is string a before string b:
string a, b might not necessarily be in the file, and to return a boolean
string a
(any text)
string b
i have tried for an hour to do this with perl, grep, etc. but no luck
grep doesnt search for patterns over multiple lines it seems?
I want something like
boolean result = string1 (any text) string 2 (input file name)
any text can be over multiple lines (its a php file)
Try this...
result=$( awk '/string1/{f=1}/string2/ && f{print "true"}' input_file )
--ahamed
danmero
3
Another way:
result=$( awk '/string2/ && f{print "true";exit}/string1/{f++}' input_file )
thanks
im not sure about the first one, it doesnt work when the pattern occurs more than once
ill try the second one
---------- Post updated at 07:25 AM ---------- Previous update was at 07:23 AM ----------
what does the {f++} mean?
---------- Post updated at 07:29 AM ---------- Previous update was at 07:25 AM ----------
does the exit mean that it exits the program? its part of a program so it should be running still
---------- Post updated at 07:31 AM ---------- Previous update was at 07:29 AM ----------
its not working
it doesnt detect it as true when the pattern exists
Paste a proper input file with possible combinations and which pattern are you referring to...
--ahamed
when there are multiple results, $result becomes:
true
true
true
true
true
true
true
true
but i need it to say just "true"
if [ "$result" == "true" ]
string1
text
string2
string2
string2
matches string1 coming before string2
but its giving:
true
true
true
but i need it to give a single "true"
---------- Post updated at 08:03 AM ---------- Previous update was at 07:49 AM ----------
i put in
if [[ "$result" == *"true"* ]]
for now if there are multiple matches does that work?
Try this...
result=$( awk '/string1/{f=1}/string2/&&f{p=1}END{if(p==1)print "true"}' input_file )
--ahamed
---------- Post updated at 05:14 AM ---------- Previous update was at 05:09 AM ----------
Updated the code with search pattern!
--ahamed
---------- Post updated at 05:16 AM ---------- Previous update was at 05:14 AM ----------
This does meet your requirement!
exit here will only exit the awk command after printing the result.
Which is your OS? If Solaris, use nawk!
--ahamed
danmero
8
You need only one pattern match to set the value to true, after that you can exit.
Check your input and patterns again.