check if one string comes before another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting check if one string comes before another
# 1  
Old 11-26-2011
check if one string comes before another

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


Code:
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)

Last edited by vbe; 11-26-2011 at 07:03 AM..
# 2  
Old 11-26-2011
Try this...
Code:
result=$( awk '/string1/{f=1}/string2/ && f{print "true"}' input_file )

--ahamed

Last edited by ahamed101; 11-26-2011 at 08:15 AM.. Reason: updated code
# 3  
Old 11-26-2011
Another way:
Code:
result=$( awk '/string2/ && f{print "true";exit}/string1/{f++}' input_file )

# 4  
Old 11-26-2011
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
# 5  
Old 11-26-2011
Paste a proper input file with possible combinations and which pattern are you referring to...

--ahamed
# 6  
Old 11-26-2011
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?
# 7  
Old 11-26-2011
Try this...
Code:
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 ----------

Quote:
Originally Posted by danmero
Another way:
Code:
result=$( awk '/string2/ && f{print "true";exit}/string1/{f++}' input_file )

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

Last edited by ahamed101; 11-26-2011 at 09:14 AM.. Reason: Updated code
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cant check empty string

Hello So i have that script collection, in which i have a single script to create a configuration file. In there, i have multiple occourences of something like this: prj_title=$(tui-read "What is the TITLE? ($prj_name):") ] && prj_title="${prj_name/_/ }" They all work as expected, if... (5 Replies)
Discussion started by: sea
5 Replies

2. Shell Programming and Scripting

Check for rows with particular string

Hi. The data file is as below: 2000922111111100232091212098324.... 2123011230912832094820943684896.... 3435983453409583405938453049583.... . . . I need to get only the rows that match my criteria. For example: those at characters 5-10 should equal to "922111" (thus getting only the 1st... (7 Replies)
Discussion started by: daytripper1021
7 Replies

3. Shell Programming and Scripting

Check file for string existence before appending it with string

I want to append file with a string but before doing that i want to check if this string already exist in that file.I tried with grep on Solaris 10 but unsuccessful.Man pages from grep seems to suggest if the string is found command status will be 0 and if not 1.But i am not finding it.May be i... (2 Replies)
Discussion started by: sahil_shine
2 Replies

4. Shell Programming and Scripting

A nice way to check a string

Guys, I need some advice on how to check a string, which may or may not have a entry.. Never really worked out how to do this.. May be a good time to learn now. This is what i am trying to do Run a command, to return a string If the string is not empty , then run the if statement,... (4 Replies)
Discussion started by: Junes
4 Replies

5. Shell Programming and Scripting

check if a string is numeric

I checked all the previous threads related to this and tried this. My input is all numbers or decimals greater than zero everytime. I want to check the same in the korn shell script. Just validate the string to be numeric. This is what I am doing. var="12345" if ) -o "$var" !=... (14 Replies)
Discussion started by: megha2525
14 Replies

6. UNIX for Advanced & Expert Users

check if a variable contains a string

hi I have an if condition that states: if ; then exit how to translate this? $x is a path $y is a string that comes at the end of the path thx (11 Replies)
Discussion started by: melanie_pfefer
11 Replies

7. Shell Programming and Scripting

how to check string of character

how can i check whether variable contains only character from a-z or A-Z....if my variable contains any alpha numeric, numeric or any character with some special one i.e. *%&@! etcetera etcetera....then it should show me please enter only characters...... Let my variable var1="abc77}|" then... (9 Replies)
Discussion started by: manas_ranjan
9 Replies

8. Shell Programming and Scripting

read string, check string length and cut

Hello All, Plz help me with: I have a csv file with data separated by ',' and optionally enclosed by "". I want to check each of these values to see if they exceed the specified string length, and if they do I want to cut just that value to the max length allowed and keep the csv format as it... (9 Replies)
Discussion started by: ozzy80
9 Replies

9. Shell Programming and Scripting

How to check a string in the variable

hi, I have a variable var1 as follows in the script. var1="one two three desformat=PDF xyz" I would like to check whether $var1 has a string "desformat=PDF" or not. Is there any command I can use (not need to creat a file)? Currently, I am using this: if ( grep "desformat=PDF"... (1 Reply)
Discussion started by: josephwong
1 Replies
Login or Register to Ask a Question