awk variable regexp works in AIX but not in SunOS?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users awk variable regexp works in AIX but not in SunOS?
# 1  
Old 06-07-2012
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:

Code:
$ cat test.ksh
#! /bin/ksh
print "Executed on OS: $( uname -rsv )" > test.out
( echo "line 1"; echo "middle line"; echo "last one" ) |
awk '
  BEGIN {
    re="iddl"
  }
  $0 ~ re {
    print "From awk: " $0
  }
' >> test.out 2>&1
$ ./test.ksh
$ cat test.out
Executed on OS: AIX 3 5
From awk: middle line

When I run the same script SunOS I get:
Code:
$ cat test.out
Executed on OS: SunOS 5.10 Generic_141414-07
awk: syntax error near line 5
awk: bailing out near line 5

Further, if I change $0 ~ re in the script to $0 ~ /iddl/, then it runs fine on SunOS (and of course AIX too). The problem is, I need a *variable* on the right hand side of the comparison, not a constant:

Code:
$ cat test_const.ksh
#! /bin/ksh
print "Executed on OS: $( uname -rsv )" > test_const.out
( echo "line 1"; echo "middle line"; echo "last one" ) |
awk '
  $0 ~ /iddl/ {
    print "From awk: " $0
  }
' >> test_const.out 2>&1
$ ./test_const.ksh
$ cat test_const.out
Executed on OS: SunOS 5.10 Generic_141414-07
From awk: middle line


Last edited by Scrutinizer; 06-07-2012 at 06:06 PM.. Reason: code tags
# 2  
Old 06-07-2012
Try using nawkor /usr/xpg4/bin/awk on Solaris.
# 3  
Old 06-07-2012
BTW I get the same errors in SunOS (and success in AIX) if I try to use the match function
match($0,re) { ...

---------- Post updated at 04:50 PM ---------- Previous update was at 04:47 PM ----------

yeah, nawk works on both systems. Why not consistent behavior with awk?
# 4  
Old 06-07-2012
Did you try both nawk and /usr/xpg4/bin/awk?

---------- Post updated at 03:55 PM ---------- Previous update was at 03:51 PM ----------

Because one of the main advantages of Solaris is it's backward compatibility. This is why sometimes outdated version of tools are in the default search path, but you always have choice to use nawk or xpg4 version if you need it's features.
# 5  
Old 06-07-2012
/usr/xpg4/bin/awk works on Solaris but is not installed on my AIX system. So nawk gives me consistent behavior and is installed on both systems.
# 6  
Old 06-09-2012
had some issues with this too...thanks for the advice
# 7  
Old 06-10-2012
Quote:
Originally Posted by charles_n_may
/usr/xpg4/bin/awk works on Solaris but is not installed on my AIX system.
See this post for an explanation. AIX achieves backwards compatibility by other means and has the POSIX-compliant versions of commands under "/usr/bin" or "/bin", while Solaris has them under "/usr/xpg4/bin".

You might want to put something like:

Code:
case $OS in
     Solaris)
          PATH=$PATH:/usr/xpg4/bin
          ;;

     AIX)
          PATH=$PATH:/usr/bin
          ;;

esac

into the environment section of your scripts. I have a separate environment file for scripts which is full of such statements and declarations, which i source in (dot-run) at the beginning of all my scripts.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

PING to AIX works but TELNET FTP SSH doesn't work

root@PRD /> rsh DR KFAFH_DR: protocol failure due to unexpected closure from server end root@PRD /> telnet DR Trying... Connected to DR. Escape character is '^]'. Connection closed. root@PRD /> ftp DR Connected to KFAFH_DR. 421 Service not available, remote server has closed connection... (2 Replies)
Discussion started by: filosophizer
2 Replies

2. Cybersecurity

ID Management Security guideline for Linux, AIX, SunOS and HP UX

I'm searching for COMPREHENSIVE ID management security guideline to manage user administration in my current job. I couldn't find it online or the books that could help. What I need to know: User security directories and how to use it. How user should be managed. How the standard user... (0 Replies)
Discussion started by: blinkingdan
0 Replies

3. Shell Programming and Scripting

awk regexp to print repetitive pattern

How to use regexp to print out repetitive pattern in awk? $ awk '{print $0, "-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-"}' output: - - - - - - - - - - - -I tried following which does not give what I want, of course. awk '{print $0, "-\t{11}-"}' output: - ... (10 Replies)
Discussion started by: yifangt
10 Replies

4. AIX

CDE on AIX and vnc,nothing works

On other unix systems,simply set "dtsession" on .xstartup file of vnc start the cde session. On aix i have this problem when i start the session,appear only an error message The DT messaging system could not be started to correct the problem 1. Choose ok to return to the login... (2 Replies)
Discussion started by: Linusolaradm1
2 Replies

5. Shell Programming and Scripting

awk regex expression works in AIX but not in Linux

I have following expression: echo "Sun 12 Jul BST 2014\nSun 12 Jul 2014\nSun 12 Jul IS 2014" | awk '/(Sun)+( 12)+( Jul )+({3} )?(2014)/{print;}' I ran above code in AIX box and output is as follows Sun 12 Jul BST 2014 Sun 12 Jul 2014 I ran above code in Linux box and output is as... (8 Replies)
Discussion started by: kamlesh_pradhan
8 Replies

6. Shell Programming and Scripting

variable assigment not works in shell script

Hi, The following assigment is not working within shell script but is working from command line. Could anybody advise why? OS - solaris 8 APPL=`grep "$Application" ldapapps |awk '{print $1}'` echo $APPL (5 Replies)
Discussion started by: urello
5 Replies

7. Shell Programming and Scripting

Sed script not working properly on Solaris (works fine on AIX)?

Hi, I have a problem with a SED script that works fine on AIX but does not work properly on a Solaris system. The ksh script executes the SED and puts the output in HTML in tables. But the layout of the output in HTML is not shown correctly(no tables, no color). Can anyone tell if there is... (7 Replies)
Discussion started by: Faith111
7 Replies

8. Shell Programming and Scripting

replace awk with a perl one liner (REGEXP and FS)

hello, I want to replace awk with a perl one liner in unix. i use in awk REGEX and FS ( field separator) because awk syntaxes in different unix os versions have not the same behaviour. Awk, Nawk and GNU Awk Cheat Sheet - good coders code, great reuse i have a file named "file" and want... (5 Replies)
Discussion started by: bora99
5 Replies

9. Shell Programming and Scripting

Comparison between variable and regexp

Hi all, How I could compare variable and regexp? For example... I am running the script ./aaa.sh -b * * is variable $2 (second argument of script). I would compare variables $a (generated from my cycle) and $2 as regexp. I need from regular expression find file that satisfies this regexp.... (1 Reply)
Discussion started by: Koblenc
1 Replies

10. 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
Login or Register to Ask a Question