OS differences in simple pattern match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting OS differences in simple pattern match
# 1  
Old 02-05-2010
OS differences in simple pattern match

Hi folksSorry if code tags don't work out correctly but this PC does not have Java setup correctly to allow me to put them inproperly.I have a simple string pattern match behaving differntly on AIX and Solaris 10 and I don't understand why or what to do about it.This simple test: -
Code:
[[ "iNSET"  == +([A-Z]) ]] && echo yes

echos yes on Solaris but not on AIX. To my mind I would expect this to fail on both as the string begins with a lower case i. This does not seem to happen on Solaris 8I am simply trying to confirm that the string begins with an uppercase letter but cannot seem to nail down a pattern that works on both OS'sAny ideas?
# 2  
Old 02-05-2010
Hi.

It's not usually the OS that is the issue in cases like this, but rather the shell. Here is a similar test:
Code:
#!/usr/bin/env ksh

# @(#) s1       Demonstrate extglob (bash term).

# Infrastructure details, environment, commands for forum posts. 
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo ; echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
c=$( ps | grep $$ | awk '{print $NF}' )
version >/dev/null 2>&1 && s=$(_eat $0 $1) || s=""
[ "$c" = "$s" ] && p="$s" || p="$c"
version >/dev/null 2>&1 && version "=o" $p
set -o nounset
echo

[[ "iNSET"  == +([A-Z]) ]] && echo OK || echo KO

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: SunOS, 5.10, i86pc
ksh M-11/16/88i

KO

and I think you were looking for a failure -- which I called "KO" in this script.

What shells are you using in both OSs? ... cheers, drl
# 3  
Old 02-07-2010
Thanks

Turned out the problem was the range A-Z, it was happy with a full sequence of letters. So I just wrote a function with the line: -

Code:
[[ "iNSET"  == +([ABCDEFGHIJKLMNOPQRSTUVWXYZ]) etc

Thanks for the feedback
# 4  
Old 02-07-2010
Hi.

I don't know why that would be the case. This style of test is by default an anchored match, meaning that the pattern must account for all characters from first through last. Sometimes that might require a "*" at one or both ends.

Here is a more complete test, using the original expression, the new one you found that worked, and a bashism that allows "normal" regular expression syntax. I then ran it with the 3 common shells, ksh, bash, zsh:
Code:
#!/usr/bin/env ksh

# @(#) s4       Demonstrate pattern matching in shell.

# Infrastructure details, environment, commands for forum posts. 
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo ; echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
c=$( ps | grep $$ | awk '{print $NF}' )
version >/dev/null 2>&1 && s=$(_eat $0 $1) || s=""
[ "$c" = "$s" ] && p="$s" || p="$c"
version >/dev/null 2>&1 && version "=o" $p
set -o nounset
echo

# Set up flags for bash and zsh, use these if variable c not set.
# ps $$ | egrep 'bash' > /dev/null && shopt -s extglob
# ps $$ | egrep 'zsh' > /dev/null && setopt KSH_GLOB
if [ "$c" = bash ]
then
  shopt -s extglob
elif [ "$c" = zsh ]
then
  setopt KSH_GLOB
elif [ "$c" = ksh -o "$c" = pdksh ]
then
  :
else
  echo " Unknown shell \"$c\" being used, aborting." >&2
  exit 1
fi

echo " With ==, i: expect KO"
[[ "iNSET"  == +([A-Z]) ]] && echo OK || echo KO

echo
echo " With ==, i, full set: expect KO"
[[ "iNSET"  == +([ABCDEFGHIJKLMNOPQRSTUVWXYZ]) ]] && echo OK || echo KO

echo
echo " With ==, X: expect OK"
[[ "XNSET"  == +([A-Z]) ]] && echo OK || echo KO

echo
echo " With ==, i, * on each end, defeating default anchors, expect OK"
[[ "iNSET"  == *+([A-Z])* ]] && echo OK || echo KO

# Do the =~ tests only for zsh and bash.

if [[ "$c" == bash || "$c" == zsh ]]
then
  echo
  echo " zsh/bashism, =~, as in regex(3), expect OK, no anchors specified:"
  [[ "iNSET" =~ [A-Z]* ]] && echo OK || echo KO

  echo
  echo " zsh/bashism, =~, as in regex(3), expect KO, anchors specified:"
  [[ "iNSET" =~ ^[A-Z]*$ ]] && echo OK || echo KO
fi

exit 0

producing for ksh:
Code:
% ./s4

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
ksh 93s+

 With ==, i: expect KO
KO

 With ==, i, full set: expect KO
KO

 With ==, X: expect OK
OK

 With ==, i, * on each end, defeating default anchors, expect OK
OK

with bash:
Code:
% bash s4

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
GNU bash 3.2.39

 With ==, i: expect KO
KO

 With ==, i, full set: expect KO
KO

 With ==, X: expect OK
OK

 With ==, i, * on each end, defeating default anchors, expect OK
OK

 zsh/bashism, =~, as in regex(3), expect OK, no anchors specified:
OK

 zsh/bashism, =~, as in regex(3), expect KO, anchors specified:
KO

with zsh:
Code:
% zsh s4

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
zsh 4.3.6

 With ==, i: expect KO
KO

 With ==, i, full set: expect KO
KO

 With ==, X: expect OK
OK

 With ==, i, * on each end, defeating default anchors, expect OK
OK

 zsh/bashism, =~, as in regex(3), expect OK, no anchors specified:
OK

 zsh/bashism, =~, as in regex(3), expect KO, anchors specified:
KO

This all worked as I expected. I noted earlier that it worked as expected on Solaris / ksh 88i.

I also ran it with pdksh, with the first tests running as expected (it does not have the machinery to process "=~", apparently) ... cheers, drl

Last edited by drl; 02-07-2010 at 02:23 PM..
# 5  
Old 02-08-2010
Thanks, that's really comprehensive.

On investigation I found that on the solaris 10 boxes, (all servers / OS's were running ksh 88) LC_ALL was unset on login.

Setting it to LC_ALL=C ksh made the test behave as you would expect, while unsetting it gave the unexpected behaviour when expanding the range A-Z. Interestingly the range 0-9 worked perfectly, didn't think to try the range a-z, maybe tomorrow when I go back to work.

I am trying to see what impact this may have on our toolset, as we don't want to adversly impact the applications we are monitoring.

Thanks for the help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

2. Shell Programming and Scripting

Rearrange or replace only the second line after pattern match or pattern match

Im using the command below , but thats not the output that i want. it only prints the odd and even numbers. awk '{if(NR%2){print $0 > "1"}else{print $0 > "2"}}' Im hoping for something like this file1: Text hi this is just a test text1 text2 text3 text4 text5 text6 Text hi... (2 Replies)
Discussion started by: invinzin21
2 Replies

3. Shell Programming and Scripting

sed : match one pattern then the next consecutive second pattern not working

Ive used this snippet of code on a solaris box thousands of times. But it isnt working on the new linux box sed -n '/interface LoopBack0/{N;/ ip address /p;}' *.conf its driving me nuts !! Is there something Im missing ? (7 Replies)
Discussion started by: popeye
7 Replies

4. Shell Programming and Scripting

Pattern match exclusive return pattern/variable

I have an application(Minecraft Server) that generates a logfile live. Using Crontab and screen I send a 'list' command every minute. Sample Log view: 2013-06-07 19:14:37 <Willrocksyea1> hello* 2013-06-07 19:14:41 <Gromden29> hey 2013-06-07 19:14:42 Gromden29 lost connection:... (1 Reply)
Discussion started by: gatekeeper258
1 Replies

5. UNIX for Dummies Questions & Answers

Match Pattern after certain pattern and Print words next to Pattern

Hi experts , im new to Unix,AWK ,and im just not able to get this right. I need to match for some patterns if it matches I need to print the next few words to it.. I have only three such conditions to match… But I need to print only those words that comes after satisfying the first condition..... (2 Replies)
Discussion started by: 100bees
2 Replies

6. Shell Programming and Scripting

Awk to match a pattern and perform a search after the first pattern

Hello Guyz I have been following this forum for a while and the solutions provided are super useful. I currently have a scenario where i need to search for a pattern and start searching by keeping the first pattern as a baseline ABC DEF LMN EFG HIJ LMN OPQ In the above text i need to... (8 Replies)
Discussion started by: RickCharles
8 Replies

7. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

8. Shell Programming and Scripting

AWK match $1 $2 pattern in file 1 to $1 $2 pattern in file2

Hi, I have 2 files that I have modified to basically match each other, however I want to determine what (if any) line in file 1 does not exist in file 2. I need to match column $1 and $2 as a single string in file1 to $1 and $2 in file2 as these two columns create a match. I'm stuck in an AWK... (9 Replies)
Discussion started by: right_coaster
9 Replies

9. Shell Programming and Scripting

Simple Pattern Match

Hello ! Experts, I saw a ton of postings here about Awk pattern matching and even after going through all of it, what I have concocted isnt working for me. Here is what I am after. I have a huge set of csv files and in the fifth column, I have text like this --- ANFD10239CS9 BCDD93948CS9... (5 Replies)
Discussion started by: PG3
5 Replies

10. Shell Programming and Scripting

Match first pattern first then extract second pattern match

My input file: <accession>Q91G55</accession> <name>043L_IIV6</name> <protein> <recommendedName> <location> <position position="294"/> </location> <fullName>Uncharacterized protein 043L</fullName> <accession>P18556</accession> <name>1106L_ASFB7</name> <protein> <recommendedName>... (5 Replies)
Discussion started by: patrick87
5 Replies
Login or Register to Ask a Question