matching words using regular expressions


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users matching words using regular expressions
# 1  
Old 12-27-2007
matching words using regular expressions

following file is taken as input

aaa bbb ccc
ddd eee ffff

grep -w aaa <filename> gives proper output.

grep \<\(aaa\).*\> filename :- should give output, since aaa is at begining, however i dosen't get any ouput.

Any discrepancy.

machine details:-
Linux anaconda 2.4.22-1.2115.nptlsmp #1 SMP Wed Oct 29 15:30:09 EST 2003 i686 i686 i386 GNU/Linux
# 2  
Old 12-27-2007
Hi.

The "\<" and "\>" are necessary for grep to see as part of the pattern. If you fail to quote them, the shell will remove the backslashes:
Code:
#!/usr/bin/env sh

# @(#) s1       Demonstrate grep with quoted word boundary assertions.

set -o nounset
echo

debug=":"
debug="echo"

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash

echo
FILE=${1-data1}
echo " Input file $FILE:"
cat $FILE

echo
echo " Results from grep -w:"
grep -w aaa $FILE

echo
echo " Results from grep word boundary assertions quoted:"
grep "\<aaa\>" $FILE

echo
echo " Unquoted and quoted backslashes:"
echo \< and "\<"

exit 0

Producing:
Code:
% ./s1

(Versions displayed with local utility "version")
GNU bash 2.05b.0

 Input file data1:
aaa bbb ccc
ddd eee ffff

 Results from grep -w:
aaa bbb ccc

 Results from grep word boundary assertions quoted:
aaa bbb ccc

 Unquoted and quoted backslashes:
< and \<

Quoting can be confusing until one practices by writing a lot of commands and shell scripts ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

regular expression matching whole words

Hi Consider the file this is a good line when running grep '\b(good|great|excellent)\b' file5 I expect it to match the line but it doesn't... what am i doing wrong?? (ultimately this regex will be in a awk script- just using grep to test it) Thanks, Storms (5 Replies)
Discussion started by: Storms
5 Replies

2. Shell Programming and Scripting

Regular Expressions

Hi Ilove unix and alwyas trying to to learn unix,but i am weak in using regular expressions.can you please give me a littel brief discription that how can i understand them and how to use .your response could lead a great hand in my unix love. (1 Reply)
Discussion started by: manoj attri
1 Replies

3. 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

4. Shell Programming and Scripting

Regular expressions help

need a regex that matches when a number has a zero (0) at the end of it so like 10 20 120 30 330 1000 and so on (6 Replies)
Discussion started by: linuxkid
6 Replies

5. UNIX for Dummies Questions & Answers

Regular expressions

In regular expressions with grep(or egrep), ^ works if we want something in starting of line..but what if we write ^^^ or ^ for pattern matching??..Hope u all r familiar with regular expressions for pattern matching.. (1 Reply)
Discussion started by: aadi_uni
1 Replies

6. 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

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. Programming

regular expressions in c++

How do I use the regular expressions in c++? (2 Replies)
Discussion started by: szzz
2 Replies

10. Shell Programming and Scripting

Regular Expressions

I'm trying to parse RichText to XML. I want to be able to capture everything between the '/par' tag in the RTF but not include the tag itself. So far all I have is this, '.*?\\par' but it leaves '\par' at the end of it. Any suggestions? (1 Reply)
Discussion started by: AresMedia
1 Replies
Login or Register to Ask a Question