awk equivalent of regex


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk equivalent of regex
# 1  
Old 01-18-2012
awk equivalent of regex

Hi all,

Can someone tell me what's the (g)awk equal of this simple regex to find ip addresses in urls:

Code:
egrep "^http://[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(:[0-9]{1,5})?/"

Input:
Code:
http://10.0.0.1/query.exe
http://11y10x09w:80/howaboutme
http://192.168.100.190:1234/takeme.gpg

Output:
Code:
http://10.0.0.1/query.exe
http://192.168.100.190:1234/takeme.gpg

I've been breaking my head over the last couple of hours trying to figure out how to define range in awk like this. {1,3} Smilie
# 2  
Old 01-18-2012
Code:
awk 'match($0,"http://[0-9]+.[0-9]+.[0-9]+.[0-9]+.")' infile

--ahamed
# 3  
Old 01-18-2012
Thanks for the reply. The code doesn't work properly:

Code:
http://12345.678910.11121314.com/howaboutme

That is technically not an IP [ valid or not ], but your script accepts it.
# 4  
Old 01-18-2012
hi.. try this ...

input - filename is f7

Code:
 
http://10.0.0.1/query.exe
http://11y10x09w:80/howaboutme
http://192.168.100.190:1234/takeme.gpg
http://1923.168.100.190:1234/takeme.gpg

script
Code:
 
awk '{ if($0 ~ /^http:\/\/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(:[0-9]{1,5})?/) print $0}' f7

output
Regards,
A!
# 5  
Old 01-18-2012
Hi A!,

For some reason, your code doesn't give me any output on my machine. I'm running Ubuntu 10.04 with GNU Awk 3.1.6 Smilie
# 6  
Old 01-18-2012
Quote:
Originally Posted by r4v3n
Hi A!,

For some reason, your code doesn't give me any output on my machine. I'm running Ubuntu 10.04 with GNU Awk 3.1.6 Smilie
From gawk documentation :
Quote:
However, because old programs may use ‘{' and ‘}' in regexp constants, by
default gawk does not match interval expressions in regexps. If either ‘--posix'
or ‘--re-interval' are specified (see Section 11.2 [Command-Line Options],
page 177), then interval expressions are allowed in regexps.
For new programs that use ‘{' and ‘}' in regexp constants, it is good practice
to always escape them with a backslash. Then the regexp constants are valid
and work the way you want them to, using any version of awk.2
Try :
Code:
awk '{ if($0 ~ /^http:\/\/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]{1,3}\.[0-9]\{1,3\}(:[0-9]\{1,5\})?/) print $0}' f7


I test the original awk command with some versions of awk :
  • Solaris /usr/bin/awk : No output
  • Solaris /usr/bin/nawk : No output
  • Solaris /usr/xpg4/bin/awk : Ok.
  • Aix : /usr/bin/awk : Ok.


Jean-Pierre.
These 2 Users Gave Thanks to aigles For This Post:
# 7  
Old 01-18-2012
Thanks Jean.

This code worked :

Code:
awk --posix '/^http:\/\/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(:[0-9]{1,5})?\// { print $0 }' filename

Apologies for not RTFM properly Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk script Equivalent .

Hello. I wrote some code for an awk command but I want to learn to turn it into an awk script but am stuck. I have a file (data.csv) that has the following data: ADD,1,3,5,8,10,11,54 SUB,1,2,3,4 ADD,15,18,21,42,37 ADD,1,1,1,0,0,3,16 ADD,4,1,8,0,4,6,13,16,17,20,8,6,4 SUB,13,8If the line... (5 Replies)
Discussion started by: Eric7giants
5 Replies

2. Shell Programming and Scripting

sed Equivalent for awk/grep

Any equivalent command using awk or grep? sed -n "/^$(date --date='10 minutes ago' '+%b %_d %H:%M')/,\$p" /abc.log (7 Replies)
Discussion started by: timmywong
7 Replies

3. Shell Programming and Scripting

Java - Arrays.binarySearch function equivalent in awk

Hi all Does anyone know Java-Arrays.binarySearch function equivalent in awk I tried like this but it's not correct one,it just returns array index if and only when searched value available in array, for some reason if searched value not found then I want to return upper nearest neighbour index.... (1 Reply)
Discussion started by: Akshay Hegde
1 Replies

4. Shell Programming and Scripting

awk equivalent code in C for printing NF

Hi all ! whether anyone in forum knows what awk will use while printing number of fields in file(NF) ? for example awk END'{print NF}' file prints number of columns in file if anyone knows equivalent code in C kindly share or explain logic behind it (8 Replies)
Discussion started by: Akshay Hegde
8 Replies

5. Shell Programming and Scripting

What is the equivalent of NR (awk) in perl?

Hello, I searched online; it seems that perl use $NR as NR in awk; however it does not work for me. For example, how to re-write the following awk using perl: awk '{ print NR}' inputfile---------- Post updated at 01:55 PM ---------- Previous update was at 12:49 PM ---------- I found... (2 Replies)
Discussion started by: littlewenwen
2 Replies

6. Shell Programming and Scripting

Regex equivalent to ! (not)

I want to have a regex line equivalent to the following: !\bfalse\b basically search for not exact string "false" (5 Replies)
Discussion started by: streetfighter2
5 Replies

7. UNIX for Dummies Questions & Answers

Using AWK and regex

Hi can you suggest in this regard The sample.txt conatins the data name lines type sam 12 txt sam 24 xls sam 36 pdf ram 32 txt ram 45 sxls ram 58 word sam 92 jpeg sam 21 gif sam 22 ltf from the data i need to sum all line... (5 Replies)
Discussion started by: krashraj
5 Replies

8. Windows & DOS: Issues & Discussions

awk to findstr equivalent

Hi, I 'd like to translate this command from awk to findstr on Windows DOS FILE: str1 server1 a str1 server2 a str2 server1 b str2 server2 b Awk command: awk ' $1 ~/str1/ { print $2, $3 } ' file.txt OUTPUT: server1 a server2 a Thanks, (1 Reply)
Discussion started by: phamp008
1 Replies

9. Shell Programming and Scripting

awk or regex

Hi! I want to made a program that will generate code like this: {{Navedi XYZ |avtor=XYZ1 |naslov=XYZ2 |leto_izzida=XYZ3 |zalozba=XYZ4 |kraj=XYZ5 |isbn=XYZ6 |cobiss_id=XYZ7 }} from input like this: <b> ODGOVORNOST............. : <a... (5 Replies)
Discussion started by: smihael
5 Replies

10. Shell Programming and Scripting

awk equivalent script

Hi All, I need a script that prints a blank line after finding that the next lines first field doesn't match the current lines first field. I really want to do this in awk to improve my own knowledge but the best I can come up with is a bash script. I'd finally like to understand how awk... (10 Replies)
Discussion started by: pondlife
10 Replies
Login or Register to Ask a Question