awk regex expression works in AIX but not in Linux


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk regex expression works in AIX but not in Linux
# 1  
Old 07-28-2014
awk regex expression works in AIX but not in Linux

I have following expression:
Code:
echo "Sun 12 Jul BST 2014\nSun 12 Jul 2014\nSun 12 Jul IS 2014" | awk '/(Sun)+( 12)+( Jul )+([A-Z]{3} )?(2014)/{print;}'

I ran above code in AIX box and output is as follows
Code:
Sun 12 Jul BST 2014
Sun 12 Jul 2014


I ran above code in Linux box and output is as follows
Code:
Sun 12 Jul 2014

What I am missing?
Is there something not according to POSIX standards.

Linux Box Detail
Code:
>uname -a
Linux ****** 2.6.18-348.3.1.el5 #1 SMP Tue Mar 5 13:19:32 EST 2013 x86_64 x86_64 x86_64 GNU/Linux


AIX box detail
Code:
# uname -a
AIX ****** 1 6 00F63E7C4C00

# 2  
Old 07-28-2014
I get this on my Linux box, the results are actually the same, my linux system just doesn't do an actually process the new line...but the results are the same.
Code:
[josephgr@oc0887178221 ~]$ echo "Sun 12 Jul BST 2014\nSun 12 Jul 2014\nSun 12 Jul IS 2014" | awk '/(Sun)+( 12)+( Jul )+([A-Z]{3} )?(2014)/{print;}'
Sun 12 Jul BST 2014\nSun 12 Jul 2014\nSun 12 Jul IS 2014

When I do this; however, on linux...
Code:
$ echo -e "Sun 12 Jul BST 2014 \n Sun 12 Jul 2014 \n Sun 12 Jul IS 2014" | awk '/(Sun)+( 12)+( Jul )+([A-Z]{3} )?(2014)/{print}'
 Sun 12 Jul 2014

-E is the default for echo on linux and that means "disable interpretation of backslash escapes"

Code:
$ uname -a
Linux oc0887178221.ibm.com 2.6.32-431.21.1.el6.x86_64 #1 SMP Tue Jun 3 19:11:40 EDT 2014 x86_64 x86_64 x86_64 GNU/Linux

# 3  
Old 07-28-2014
What's your awk version? Some Linuxes use mawk which doesn't recognize the extended regex [A-Z]{3}. And, use echo -e to have the "\n" interpreted correctly.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 07-28-2014
Most awk on Linux do not recognize [A-Z]{3} which is called an interval expression, but it can be enabled like so:

Code:
echo -e "Sun 12 Jul BST 2014\nSun 12 Jul 2014\nSun 12 Jul IS 2014" | awk --re-interval '/(Sun)+( 12)+( Jul )+([A-Z]{3} )?(2014)/{print;}'
Sun 12 Jul BST 2014
Sun 12 Jul 2014

From man page:

Code:
--re-interval Enable the use of interval expressions in regular expression matching (see Regular Expressions, below). 
Interval expressions were not traditionally available in the AWK language. The POSIX standard added them, to make awk 
and egrep consistent with each other. However, their use is likely to break old AWK programs, so gawk only provides 
them if they are requested with this option, or when --posix is specified.

# 5  
Old 07-28-2014
Thanks in2nix4life, answer is specific one....
I got the point.

But is there any other way to handle interval expression so that it will work in my AIX and Linux version both.

Because
Code:
[A-Z]{3}

is an basic functionality, hence there must be something else which can replace it in my Linux version.
# 6  
Old 07-28-2014
What's wrong with --re-interval ? It does exactly what you want...

You could also use --posix...

Also, "echo -e" is not crossplatform. Use "printf" to get consistent behavior.

Code:
printf "%s\n" "line1" "line2"

# 7  
Old 07-28-2014
Simplest solution... Wrap your awk command in a script and detect which OS the script is being executed on and run the correct awk command. 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

Regex Expression Replace

I have a XML file where there is a tag with like <wd:address_line_1>1234 Street</wd:address_line_1> I want to replace the values "1234 Street" with "Test Data". Different people have different address lines and i want to replace with a fixed value to mask the file. I was trying to use sed... (7 Replies)
Discussion started by: dr46014
7 Replies

2. Shell Programming and Scripting

awk works on Linux but fails on Solaris

On linux i have the below command working fine. awk '/<app-deploy>/{A=1;++i} /<\/app-deploy>/{print >> "found"i".tmp";A=0} A{;print >> "found"i".tmp"}' deploy.xml But the same is failing on Solaris Output: awk: syntax error near line 1 awk: bailing out near line 1 uname -a SunOS mymac 5.10... (5 Replies)
Discussion started by: mohtashims
5 Replies

3. Shell Programming and Scripting

Hi im new to bash scripting I want to know what does the regex expression do ??

# check host value regex='^(||1|2|25)(\.(||1|2|25)){3}$' if ')" != "" ]; then if ]; then echo host $host not found exit 4 fi elif ]; then echo $host is an invalid host address exit 5 fi (1 Reply)
Discussion started by: kevin298
1 Replies

4. UNIX for Advanced & Expert Users

awk variable regexp works in AIX but not in SunOS?

Using awk variables for regular expressions is working for me in AIX. It is failing for me in SunOS. I don't know why. Can someone explain and/or suggest a fix for the SunOS version? Here is a little test script. It runs fine in AIX: $ cat test.ksh #! /bin/ksh print "Executed on OS: $(... (6 Replies)
Discussion started by: charles_n_may
6 Replies

5. Shell Programming and Scripting

passing a regex as variable to awk and using that as regular expression for search

Hi All, I have a sftp session log where I am transferring multi files by issuing "mput abc*.dat". The contents of the logfile is below - ################################################# Connecting to 10.75.112.194... Changing to: /home/dasd9x/testing1 sftp> mput abc*.dat Uploading... (7 Replies)
Discussion started by: k_bijitesh
7 Replies

6. Shell Programming and Scripting

awk variables in regex expression ?

Hello, Could someone explain why this one returns nothing: $ x=/jon/ $ echo jon | awk -v xa=$x '$1~xa {print}' $ while the following works fine: $ x=jon $ echo jon | awk -v xa=$x '$1==xa {print}' $ jon and the following works fine: $ echo jon | awk '$1~/jon/ {print}' $ jon ... (3 Replies)
Discussion started by: vilius
3 Replies

7. Shell Programming and Scripting

awk -F works on Linux, but not on Solaris

Hello, I found this command works on Linux: $ echo `uptime` | awk -F "load average: " '{ print $2 }' 1.60, 1.53, 1.46 but got error on Solaris: $ echo `uptime` | awk -F "load average: " '{ print $2 }' awk: syntax error near line 1 awk: bailing out near line 1 $ which awk... (2 Replies)
Discussion started by: seafan
2 Replies

8. Shell Programming and Scripting

help with simple regex expression

I am trying to grep the following line in a file using a bash shell: (..) admin1::14959:::::: (..) It works with the following expression (as expected) # cat file | grep ^*:: admin1::14959:::::: but it does not work with (not expected) # cat /etc/shadow | grep ^+:: I assume the... (2 Replies)
Discussion started by: schms
2 Replies

9. Shell Programming and Scripting

Creating a regex expression

Good morning all!! In my code I and looking through file /etc/syslog.congf and printing every line that has /var/log in it. I need to turn the if 9$line) into a regex code instead. #!/usr/bin/perl @file= 'cat /etc/syslog.conf'; //when foreach $line (@file){ if ($line =~... (3 Replies)
Discussion started by: bigben1220
3 Replies

10. Shell Programming and Scripting

Regular expression (regex) required

I want to block all special characters except alphanumerics.. and "."(dot ) character currently am using // I want to even block only single dot or multiple dots.. ex: . or .............. should be blocked. please provide me the reg ex. ---------- Post updated at 05:11 AM... (10 Replies)
Discussion started by: shams11
10 Replies
Login or Register to Ask a Question