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?
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 2,288
Thanks Given: 430
Thanked 480 Times in 395 Posts
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
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.
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)
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)
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)
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)
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)
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)
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)
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)