How to use regexp to find an ipaddress from a query string?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use regexp to find an ipaddress from a query string?
# 1  
Old 04-22-2013
How to use regexp to find an ipaddress from a query string?

I need help with a regexp to find out the ip address which can possibly be present in a URL.

The URLs can be in any of the following form
<domain>?a=12345&d=somestring1
<domain>?c=10.10.10.100&d=somestring1
<domain>?a=12345&b=somestring1&c=10.1.2.4d=somestring2
<domain>?a=12345&c=10.1.2.4&b=somestring1&d=somestring2

The rules
1) "c=" may not always be present
2) "c=" can be anywhere in the string but not the last, last is always "d"
3) If "c=" is present I need the value of c which should be an ip address.
# 2  
Old 04-22-2013
Code:
$ cat urls
<domain>?a=12345&d=somestring1
<domain>?c=10.10.10.100&d=somestring1
<domain>?a=12345&b=somestring1&c=10.1.2.4&d=somestring2
<domain>?a=12345&c=10.1.2.4&b=somestring1&d=somestring2
<domain>?a=12345&b=somestring1&d=somestring2&c=10.1.2.9

Code:
$ sed -n "s/.*\(c=[^&]*\).*d=.*/\1/p" urls
c=10.10.10.100
c=10.1.2.4
c=10.1.2.4

# 3  
Old 04-22-2013
Thanks Hanson.

Is it possible to do this in tcl ? Need only ip address without the "c="

For eg,

set urlvar <one of the URLs mentioned earlier>

set ipout [regexp "s/.*\(c=[^&]*\).*d=.*/\1/p" $urlvar]

puts "$ipout"
# 4  
Old 04-22-2013
Yes, I think you should be able to apply the logic to TCL commands, since TCL supports regular expressions. I don't know anything about the TCL regexp command, so cannot help with details. Any chance TCL can call sed?

Yes, you can get the ip address, minus the c= part, by moving c= outside of the saved group, as follows:
Code:
$ sed -n "s/.*c=\([^&]*\).*d=.*/\1/p" urls
10.10.10.100
10.1.2.4
10.1.2.4

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

find and Replace String in Perl - Regexp

Trying to find and replace one string with another string in a file #!/usr/bin/perl $csd_table_path = "/file.ntab"; $find_str = '--bundle_type=021'; $repl_str = '--bundle_type=021 --target=/dev/disk1s2'; if( system("/usr/bin/perl -p -i -e 's/$find_str/$repl_str/' $csd_table_path")... (2 Replies)
Discussion started by: cillmor
2 Replies

2. Shell Programming and Scripting

Regexp for string that might contain a given character

I'm probably just not thinking of the correct term to search for :-) But I want to match a pattern that might be 'ABC' or '1ABC' there might be three characters, or there might be four, but if there are four, the first has to be 1 (1 Reply)
Discussion started by: jnojr
1 Replies

3. UNIX and Linux Applications

How to find xterm ipaddress?

There is a linux server in my team where everyone is using xterm to connect to the server and work. Problem is I'm unable to find the ipaddress of the xterm user. It just shows the display as "localhost". example: st_capuk@MGTS5026-13sh1:~> ps -eaf | grep xterm 1010 9328 9327 0 May07 ?... (2 Replies)
Discussion started by: Arun_Linux
2 Replies

4. UNIX for Dummies Questions & Answers

regexp: match string that contains list of chars

Hi, I'm curious about how to do a very simple thing with regular expressions that I'm unable to figure out. If I want to find out if a string contains 'a' AND 'b' AND 'c' it can be very easily done with grep: echo $STRING|grep a|grep b|grep c but, how would you do that in a single... (9 Replies)
Discussion started by: jimcanoa
9 Replies

5. Shell Programming and Scripting

extract string until regexp from backside

Hi, I searched in the forums, but I didn't find a good solution. My problem is: I have a string like "TEST.ABC201005.MONTHLY.D101010203". I just want to have the string until the D100430, so that the string should look like: "TEST.ABC201005.MONTHLY.D" The last characters after the D can be... (8 Replies)
Discussion started by: elifchen
8 Replies

6. UNIX for Dummies Questions & Answers

find and regexp

i am totally confused now, when I use find command, why it does not take the regular express as filename? for example, i want to find out anything include word "chapter" in their file names, and i used the below command find / -name ".*chapter.*" and the system gives me nothing, although... (2 Replies)
Discussion started by: fedora
2 Replies

7. Shell Programming and Scripting

Help regarding behavior sed regexp query

Hi all, I have one question regarding sed regexp (or any regexp in general), I have some path like this C:/Abc/def/ghi/jkl in a file file1 Now if i use following code cat file1 | sed 's#\(.*\)/.*#\1#' Now it give me following output C:/Abc/def/ghi, which is fine But i just... (2 Replies)
Discussion started by: sarbjit
2 Replies

8. UNIX for Dummies Questions & Answers

How to find out the ipaddress?

Hi Friends, I am new to UNIX. I wonder how can I get the ipaddress of my machine? In windows, i can use ipconfig to get my ipaddress. I am aware of ifconfig but it does not give the ipaddress.:) Thanks and regards, Dinesh Venkatesan. (4 Replies)
Discussion started by: DineshV
4 Replies

9. Shell Programming and Scripting

regexp to get first line of string

Hi everybody for file in * #Bash performs filename expansion #+ on expressions that globbing recognizes. do output="`grep -n "$1" "$file"`" echo "$file: `expr "$output" : '\(^.*$\)'`" done In the above bash script segment, I try to print just the first line of string named... (3 Replies)
Discussion started by: jonas.gabriel
3 Replies

10. HP-UX

how to find ipaddress

Hi, Pls tell me any command in Hp-unix to find out the machine ipaddress Thanks, shruti (14 Replies)
Discussion started by: shruti_mgp
14 Replies
Login or Register to Ask a Question