puzzling regexp


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting puzzling regexp
# 1  
Old 06-29-2006
Question puzzling regexp

hey guys,

i'm having some problems with my understanding of this whole regexp thing.

I'm just exploring here really by trying to do various match & filter & print stuff on the console.

I figured i want to get the IP of an interface. So my idea here was that first i filter to extract only the line with "inet" from ifconfig. Hence i type :

ifconfig rl0 | grep inet

this yields the two lines of ipv4 and ipv6 address configuration. Then i only take the 2nd field of each line since that's where the address really is hence i type :

ifconfig rl0 | grep inet | awk '{print $2}'

and i end up with both the ipv4 and ipv6 address so that's where it gets tricky for me. I thought that i'd put a last filter with a regexp to get the line that begins with a word made up of at least 1 and at most 3 digits:

ifconfig rl0 | grep inet | awk '{print $2}' | grep '^[0-9]{1,3}'

and it doesn't work, ie i end up with a void set. However if i remove the {1,3} quantifier, it does work Smilie and i get the ipv4 address !

I don't understand this at all. Why does it work with only one occurence but if i say at least 1 and at most 3 it just fails Smilie !!! Is there something flawed in my understanding of regexps ? Can someone provide some insights ?

PS:
i don't think it matters but just in case, i'm using tcsh

Last edited by jad; 06-29-2006 at 07:09 PM..
# 2  
Old 06-29-2006
It depends on whether the version of grep you are using understands extended regex.

Meybe this would work better?

Code:
ifconfig rl0 |  awk '/inet / {print $2}'

# 3  
Old 06-30-2006
Try something like this:
Code:
ifconfig rl0 | gawk --re-interval '/^[0-9]{1,3} .*inet/ {print $2}'

If you're using awk, you seldom need to use grep. Smilie
# 4  
Old 06-30-2006
Use egrep

Use egrep if you have it indstalled ( hint: which egrep)

ifconfig | awk '/inet/{ print $2}' | egrep "[0-9]{1,3}"
# 5  
Old 07-01-2006
Thanks guys for your help.

I should've mentionned earlier that i was more interested in understanding why my command didn't work than in how actually getting the IP.

So anyway Reborg you were right it has to do with grep only understanding ultra basic regexp and as Vish_indian said, egrep is the way to go in that case.

So by using egrep it just works fine !

PS:
i need to work more on awk i guess Smilie...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regexp

I would like to extract "1333 Fairlane" given the below text. The word "Building:" is always present. The wording between Building and the beginning of the address can be almost anything. It appears the the hyphen is there most of the time. Campus: Fairlane Business Park Building:... (9 Replies)
Discussion started by: bbaker@copesan.
9 Replies

2. UNIX for Dummies Questions & Answers

Puzzling permission issue

I have a file, the long listing output by 'ls -l' is -rw-r--r-- 1 usera agroup 1246 Jul 7 14:44 temp.R The file is under a Solaris ZFS file system. As a different user (userb), I did cp temp.R /tmp ls -l /tmp/temp.R -rw-r--r-- 1 userb agroup 1246 Nov 16 14:45 /tmp/temp.R ... (14 Replies)
Discussion started by: nugulus
14 Replies

3. Shell Programming and Scripting

special grep on log files... puzzling me.

Okay, guys, I've got one: I've got a script that formats all of the messages in a log file. It works on a completely verbose method. I'm trying to write a filter that will search for a bunch of case-insensitive strings, but also, always print the first 2 lines. Here's the egrep part: ... (4 Replies)
Discussion started by: quirkasaurus
4 Replies

4. Programming

Linux System C headers puzzling

I am new to Linux system programming, and I found the sophisiticated definition of some library function differ a lot with what I learnt in classes. Here is the question: what does the suffix of function `chmod' in sys/stat.h mean. The funtion claimation is like following: extern int... (5 Replies)
Discussion started by: JackCrital2005
5 Replies

5. IP Networking

Puzzling Traceroute output

This is the output that I get every so often when trying to do a traceroute to the web server whenever it's inaccessible: traceroute to 64.40.98.181 (64.40.98.181), 30 hops max, 40 byte packets 1 207.97.207.194 (207.97.207.194) 2.625 ms 2.840 ms 2.968 ms 2 core1-5.iad1.rackspace.com... (6 Replies)
Discussion started by: gaspol
6 Replies

6. Shell Programming and Scripting

Syntax error, puzzling

I am just learning over here unix scripting and the OS in general. I wrote this script (with some great help from you guys here) when I test at my desktop with cygwin it works fine, but when I take it to work and try it on the Solaris 2 system there I get an error on line 4 syntax error... (4 Replies)
Discussion started by: Fred Goldman
4 Replies

7. UNIX for Dummies Questions & Answers

print the line immediately after a regexp; but regexp is a sentence

Good Day, Im new to scripting especially awk and sed. I just would like to ask help from you guys about a sed command that prints the line immediately after a regexp, but not the line containing the regexp. sed -n '/regexp/{n;p;}' filename What if my regexp is 3 word or a sentence. Im... (3 Replies)
Discussion started by: ownins
3 Replies

8. Shell Programming and Scripting

Puzzling Problem: Going back to the previous directory

Hi, I am trying to figure out if there is a way to make linux take me back to the previous directory I was working in. For instance, I am in /home/username/directory1 Then if I cd into /home/username/directory1/temp1/temp2/temp3 I would like to immediately go back to the previous... (2 Replies)
Discussion started by: Legend986
2 Replies

9. Shell Programming and Scripting

regexp help

I'd like to know if there is a catchall line for renaming the following patterns: s01e03 -> 01x03 s4e9 -> 04x09 s10e08 ->10x08 and possibly even: 318 -> 03x18 1002 ->10x02 if its the first 3 or first digit number in the string. thanks! (0 Replies)
Discussion started by: TinCanFury
0 Replies

10. Shell Programming and Scripting

AWk is still puzzling me...

Hi all. Here's what i want to do: my_first_file #!/usr/bin/ksh # BLA BLA BLA BLA printf "this is my first file \n" echo " I understand this far" printf "the day's over \n" echo "great" my_second_file #!/usr/bin/ksh # BLA BLA BLA BLA printf "this is my first file \n" AN_INSERTED_LINE... (2 Replies)
Discussion started by: penguin-friend
2 Replies
Login or Register to Ask a Question