grep and regex question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep and regex question
# 1  
Old 07-03-2008
grep and regex question

basically i have a csv i parse through. a user will supply me with a san switch he/she wants more info about... say the name is "pnj-sansw124"

now i can grep out every connection to that switch w/o issue because this sans switch pnj-sansw124 has multiple slots 1-10. and it looks like this in the csv "pnj-sansw124-1/23" where 1/23 is slot/port. so...

switch=pnj-sansw124
slot=1

Code:
grep -w $switch"-"$slot $file1 | awk -F, '{ print $2,$7 }' | while read cl1 cl2
do
    .
    .
    .
done

this works just fine

however i run into a problem where there is a san switch with no slots. so i would like to grep with a regex that would allow me to look for the "pnj-sansw124" but look for the "-[0-9]/" extension as well. so if its there, then continue to ask for slot. if not, go to a different section of code for this guy. a no slot san switch would look like "pnj-sansw110-58" where 58 is obviousy just the port.

i tried something like "pnj-sansw124-`([0-9]\{1\}|[0-9]\{2\})`//" but that didn't work out right.

thoughts?

-pupp
# 2  
Old 07-03-2008
You need to use egrep or grep -E to have extended regular expression support.
# 3  
Old 07-08-2008
i tried egrep but not working for me.

this is ksh btw. sorry if it wasn't posted earlier.

Code:
egrep pnj-sansw124-'([0-9]\{1\}|[0-9]\{2\})' io1 | while read jerk
do
        echo $jerk
done

basically getting nothing at this point.
# 4  
Old 07-08-2008
Well, while testing this I think I'm running into a grep bug on HP-UX... it's working fine for me on Linux. What operating system are you using exactly?

Anyway, this *should* work:

Code:
echo "pnj_sansw124_123" | grep -w  'pnj_sansw124_[0-9]\{1,2\}'

However it does not work on HP-UX... as soon as you add the -w it fails to match, I can't for the life of me figure out why.

If you do choose to use egrep, or grep -E, it uses the Extended Regular Expression syntax, which has a subtle difference in the repetition syntax, no backslashes required before the squiggly brackets, e.g.:

Code:
echo "pnj_sansw124_12" | grep -Ew 'pnj_sansw124_[0-9]{1,2}'

# 5  
Old 07-08-2008
running on sol10.

i ended up not using the "short cut" way with
Code:
egrep pnj-sansw124-'([0-9]\{1\}|[0-9]\{2\})'

so i changed it to
Code:
egrep pnj-sansw124-([0-9]|[0-9][0-9])/

this seems to work. however, obviously we start running into problems with higher numbers or more then two digit numbers. i don't feel like running all those ranges out. but for purposes for what i need it for now, its good. i'll fine tone it later with your suggestion.

now with grep -E, i fail. it didn't like that. egrep is good though.

thanks for the help. let me get to work and give it a go.

-pupp
# 6  
Old 07-08-2008
Well, I've tested it on my Solaris 10 box and grep appears to behave as expected there.

If you choose to use egrep, the only caveat is that the default /usr/bin/egrep doesn't support the {m,n} repetition syntax (why, oh why don't Sun ever update these utilities!?!) so you may prefer to use the less prehistoric version in /usr/xpg4/bin/egrep. Also, egrep does not support -w, so you need to surround your expression with \<...\> to match words.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep regex

Hi everyone, I'm looking for a grep command to match the following pattern from a file: <EGS>10234567<EGS> I used this following command to do this: grep -E '^<EGS>{8}<EGS>' test.txt In output I got: <EGS>10234567<EGS> Till now it work, but if I add something at the end of the line... (2 Replies)
Discussion started by: Arnaudh78
2 Replies

2. UNIX for Beginners Questions & Answers

Grep in regex

Hello guys, Here i am writing a script in bash to check for a valid URL from a file using regex This is my input file http://www.yahoo.commmmmm http://www.google.com https://www.gooogle.co www.test6.co.in www.gmail.com www.google.co htt://www.money.com http://eeeess.google.com... (2 Replies)
Discussion started by: Meeran Rizvi
2 Replies

3. Shell Programming and Scripting

Regex for filename in grep

I want to print the filename keyword="XXTNL_AVSKRIV2ING" ftype="sql' I wan to search the keyword in all the sql files and the output shoul dbe filename:count grep -iwc "$keyword" *.$ftype | grep -v ":0$" But the output does not dispaly the filename which contains space as... (4 Replies)
Discussion started by: millan
4 Replies

4. Shell Programming and Scripting

regex, awk, grep question "how to"

I've been working on this for 2 days and I'm not getting far. It is time to turn to you guys. With the data below, I am trying to create a file that looks like this: I'd like to use some form of egrep I think. AY#box#P04prod_to_contingency s AY#cmd#P04dump_cont_db s AY#cmd#P04get_on_ice_job s... (2 Replies)
Discussion started by: rawbi01
2 Replies

5. Shell Programming and Scripting

grep -v and regex

How to match lines that don't contain a patern in regex it self, without using the -v option of grep? (15 Replies)
Discussion started by: vistastar
15 Replies

6. UNIX for Dummies Questions & Answers

| help | unix | grep (GNU grep) 2.5.1 | advanced regex syntax

Hello, I'm working on unix with grep (GNU grep) 2.5.1. I'm going through some of the newer regex syntax using Regular Expression Reference - Advanced Syntax a guide. ls -aLl /bin | grep "\(x\)" Which works, just highlights 'x' where ever, when ever. I'm trying to to get (?:) to work but... (4 Replies)
Discussion started by: MykC
4 Replies

7. Shell Programming and Scripting

regex and grep

I want it to find lines that contain any number of capital letters before P this is what I have tried echo "AAAAAP" | grep 'P' echo "AAAAAP" | grep '\{1\}P' echo "AAAAAP" | grep '^*P' But none of them seem to work, any help is much appreciated thanks Calypso (4 Replies)
Discussion started by: Calypso
4 Replies

8. UNIX for Dummies Questions & Answers

Help with grep and regex

Hi all, I'm a beginner with linux, regex, grep, etc I am trying to get data out of a file that has about 13,000 lines in this format name - location I want to grep all the names out to one file and the locations to another so I can put them into a spreadsheet. Some have hyphenated... (14 Replies)
Discussion started by: raichlea
14 Replies

9. UNIX for Dummies Questions & Answers

grep with Regex help!

Hello everybody, I'd like to know how is it I should write a regex in unix to match a string not followed by another string (anywhere in the line). To be more specific, I want to find lines where "drop table" is found, but not followed anywhere in the line by the character "&". For... (3 Replies)
Discussion started by: mvalonso
3 Replies

10. UNIX for Dummies Questions & Answers

use of regex on grep

having a look on the regex site I saw that characters can be search using hex values http://www.regular-expressions.info/characters.html So I try to use it whith grep to find a è on a string (octal Decimal Hexa : 350 232 E8) but it doesn't work E.g. /usr/bin/echo '\0350' | egrep '\xE8' ... (0 Replies)
Discussion started by: solea
0 Replies
Login or Register to Ask a Question