Searching backwards using regular expressions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching backwards using regular expressions
# 1  
Old 01-21-2010
Searching backwards using regular expressions

I'm having trouble writing a regular expression that matches the text I need it to. Let me give an example to express my trouble. Suppose I have the following text:
Code:
if(condition)
	multiline
	statement
else if(condition)
	multiline
	statement
else if(condition)
	multiline
	statement
else if(condition)
	multiline
	statement
else if(condition)
	multiline
	statement
	find me
else if(condition)
	multiline
	statement

I want to find the line "find me" and match everything up from it to the nearest "else if(condition)". There can be anything at all between them, including newline characters. I tried the following regular expression:
Code:
m/else\sif\(condition\)[\s\S]*?find\sme/

Unfortunately the above regular expression does not do what I want. It finds the first "else if(condition)" and selects everything down to the first "find me" despite the nongreedy asterisk. Is there a way to match the "find me" first and then search up for for the "else if(condition)"?


Note: I know the above example doesn't make any sense code-wise. It's not the text I'm trying to match, but it's much less complicated and expresses what I'm trying to accomplish better.
# 2  
Old 01-21-2010
Should be something like:

Code:
awk '
{s=s RS $0}
/else if/{s=$0}
/find me/{print s;exit}
' file

# 3  
Old 01-22-2010
Hi.

I like utility cgrep for situations like this. It allows one to specify conveniently a regular expression for the previous and succeeding boundaries -- "windows":
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate previous boundary match with cgrep.
# http://www.bell-labs.com/project/wwexptools/cgrep/

echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1)
set -o nounset
echo

FILE=${1-data1}

echo " Data file $FILE:"
cat $FILE

echo
echo " Results:"
cgrep -D -w "else if" "find me" $FILE

exit 0

producong:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
GNU bash 3.2.39

 Data file data1:
if(condition)
	multiline
	statement
else if(condition)
	multiline
	statement
else if(condition)
	multiline
	statement
else if(condition)
	multiline
	statement
else if(condition)
	multiline
	statement
	find me
else if(condition)
	multiline
	statement

 Results:
else if(condition)
	multiline
	statement
	find me

You will need to obtain, compile, and make available cgrep. See the URL mentioned in the script for that.

Best wishes ... cheers, drl
# 4  
Old 01-22-2010
Code:
tail -r file | nawk '/find me/{p=1}(p){print}(p)&&/ if[(]/{exit}' | tail -r

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regular expressions

I need to pick a part of string lets stay started with specific character and end with specific character to replace using sed command the line is like this:my audio book 71-skhdfon1dufgjhgf8.wav' I want to move the characters beginning with - end before. I have different files with random... (2 Replies)
Discussion started by: XP_2600
2 Replies

2. Shell Programming and Scripting

Help with regular expressions

I have a file that I'm trying to find all the cases of phone number extensions and deleting them. So input file looks like: abc x93825 def 13234 x52673 hello output looks like: abc def 13234 hello Basically delete lines that have 5 numbers following "x". I tried: x\(4) but it... (7 Replies)
Discussion started by: pxalpine
7 Replies

3. Shell Programming and Scripting

searching regular expressions with special characters like dot using grep

hi everybody I am a new user to this forum and its previous posts have been very useful. I'm searching in a file using grep for patterns like 12.13.444 55.44.443 i.e. of form <digit><digit>.<digit><digit>.<digit><digit><digit> Can anybody help me with this. Thanks in advance (4 Replies)
Discussion started by: jpriyank
4 Replies

4. UNIX for Advanced & Expert Users

regular expressions

I have a flat file with the following drug names Nutropin AQ 20mg PEN Cart 2ml Norditropin Cart 15mg/1.5ml I have to extract digits that are before mg i.e 20 and 15 ; how to do this using regular expressions Thanks ram (1 Reply)
Discussion started by: ramky79
1 Replies

5. Shell Programming and Scripting

Need help with Regular Expressions

Hi, In ksh, I am trying to compare folder names having -141- in it's name. e.g.: 4567-141-8098 should match this expression '*-141-*' but, -141-2354 should fail when compared with '*-141-*' simlarly, abc should fail when compared with '*-141-*' I tried multiple things but nevertheless,... (5 Replies)
Discussion started by: jidsh
5 Replies

6. UNIX for Advanced & Expert Users

Regular Expressions

Hi, below is a piece of code written by my predecessor at work. I'm kind of a newbie and am trying to figure out all the regular expressions in this piece of code. It is really a tough time for me to figure out all the regular expressions. Please shed some light on the regular expressions... (3 Replies)
Discussion started by: ramky79
3 Replies

7. UNIX for Dummies Questions & Answers

regular expressions

how to find for a file whose name has all characters in uppercase after 'project'? I tried this: find . -name 'project**.pdf' ./projectABC.pdf ./projectABC123.pdf I want only ./projectABC.pdf What is the regular expression that correponds to "all characters are capital"? thanks (8 Replies)
Discussion started by: melanie_pfefer
8 Replies

8. Shell Programming and Scripting

Help with regular expressions

I have following content in the file CancelPolicyMultiLingual3=U|PC3|EN RestaurantInfoCode1=U|restID1|1 ..... I am trying to use following matching extression \|(+) to get this PC3|EN restID1|1 Obviously it does not work. Any ideas? (13 Replies)
Discussion started by: arushunter
13 Replies

9. Shell Programming and Scripting

Regular Expressions

How can i create a regular expression which can detect a new line charcter followed by a special character say * and replace these both by a string of zero length? Eg: Input File san.txt hello hi ... (6 Replies)
Discussion started by: sandeep_hi
6 Replies

10. Programming

regular expressions in c++

How do I use the regular expressions in c++? (2 Replies)
Discussion started by: szzz
2 Replies
Login or Register to Ask a Question