Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check string has only a-z0-9

Status
Not open for further replies.

ogniemi

Technical User
Nov 7, 2003
1,041
PL
hello,

Example with [[:alnum:]] I found in post is not working good in case german letters Ä/ä, Ö/ö, Ü/ü are in a string:

Code:
# echo 243dÜdd|awk '$1 !~ /^[[:alnum:]]+$/ {print "notalnum"}'
# echo 243dÜdd|awk '$1 !~ /^[a-zA-Z0-9]+$/ {print "notalnum"}'
notalnum
# echo 243dÜdd|awk '$1 !~ /^[a-z0-9]+$/ {print "notalnum"}'
notalnum
# echo 243ddd|awk '$1 !~ /^[a-z0-9]+$/ {print "notalnum"}'
#

I think /^[a-z0-9]+$/ is the best solution in my case if I want to prove there are only digits and/or low letter english characters in string. Am I correct? Are both "green" commands below correct solutions?

What is the meaning of {u=1}{if(u!=1) in the first green command below?
Why the red command does not output the same?

1'st: awk '$1 !~ /^[a-z0-9]+$/{u=1}{if(u!=1){print $0" - only a-z and/or 0-9"}}' input2
2'nd: awk '$1 ~ /^[a-z0-9]+$/{u=1}{if(u=1){print $0" - only a-z and/or 0-9"}}' input2

Code:
# cat input2
9pv8bdm82e8v8ga3
vp4xnbvvgu59ykz8
9uei8zr8dx4kecna
zr7n23hhvmrt7qzf
idxrdpxrkzbzby9b
pexqgf7b3w59m4n6
gjvt9sjtsut4s2dg
fb6vqc7r9ayucmyt
7tkek58cyzfrzs7f
3casnfiwpgq24pt9
nWLheuvjCsZaMK4M
34ZZcf5qVYQhqNsd
sDMW7HGJ9qsU9uqB
wQiZ5nQZMpGCB9q3
LBYSSniZcb4kVsvR
GezLTPunDAzPJpRd
zx6Skn2NP49tv4KC
gJX9gRwtawHskgWj
daCPNCkkFtdBvxMc
FtkwJhqtd7kPmG9R
97RRGD(&9SsPeUDv
SCbAgAxW=-5Ca]Tt
r}F5BL9Uw)}Z]BR*
[+JCKBsbL#z%bNwQ
M9bgcv9X}73ZTaf*
j(L2G=nmAiqx4hJ[
|#D]xW5vkERA|4Jh
=nu]$cJCJ78Xz[4w
aRUW#fNn$x&~)NhE
9Zz?=EYbWp7RLHH$
jNyw(-s&r+}3|)5G
}i*+{hUCVuW=GfA6
kK%dQvnN$jV7+Tza
wyjM8ABmUb(7}EDn
7N95mBJTxMcgy!Pb
TdXmcw*CeixWy}7|
Hhvc2VpQ9y#i[ZU=
RaX4zB(7kQMir2]S
B3u)a=EX?(4t[LF8
}T4?ZbRG+w){%gNF
# [green]awk '$1 !~ /^[a-z0-9]+$/{u=1}{if(u!=1){print $0" - only a-z and/or 0-9"}}' input2[/green]
9pv8bdm82e8v8ga3 - only a-z and/or 0-9
vp4xnbvvgu59ykz8 - only a-z and/or 0-9
9uei8zr8dx4kecna - only a-z and/or 0-9
zr7n23hhvmrt7qzf - only a-z and/or 0-9
idxrdpxrkzbzby9b - only a-z and/or 0-9
pexqgf7b3w59m4n6 - only a-z and/or 0-9
gjvt9sjtsut4s2dg - only a-z and/or 0-9
fb6vqc7r9ayucmyt - only a-z and/or 0-9
7tkek58cyzfrzs7f - only a-z and/or 0-9
3casnfiwpgq24pt9 - only a-z and/or 0-9
# awk '$1 !~ /^[a-z0-9]/{u=1}{if(u!=1){print $0" - only a-z and/or 0-9"}}' input2
9pv8bdm82e8v8ga3 - only a-z and/or 0-9
vp4xnbvvgu59ykz8 - only a-z and/or 0-9
9uei8zr8dx4kecna - only a-z and/or 0-9
zr7n23hhvmrt7qzf - only a-z and/or 0-9
idxrdpxrkzbzby9b - only a-z and/or 0-9
pexqgf7b3w59m4n6 - only a-z and/or 0-9
gjvt9sjtsut4s2dg - only a-z and/or 0-9
fb6vqc7r9ayucmyt - only a-z and/or 0-9
7tkek58cyzfrzs7f - only a-z and/or 0-9
3casnfiwpgq24pt9 - only a-z and/or 0-9
nWLheuvjCsZaMK4M - only a-z and/or 0-9
34ZZcf5qVYQhqNsd - only a-z and/or 0-9
sDMW7HGJ9qsU9uqB - only a-z and/or 0-9
wQiZ5nQZMpGCB9q3 - only a-z and/or 0-9
# awk '$1 ~ /^[a-z0-9]+$/{u=0}{if(u!=0){print $0" - only a-z and/or 0-9"}}' input2
# awk '$1 ~ /^[a-z0-9]+$/{u=0}{if(u=0){print $0" - only a-z and/or 0-9"}}' input2
# [red]awk '$1 ~ /^[a-z0-9]+$/{u=1}{if(u=1){print $0" - only a-z and/or 0-9"}}' input2[/red]
9pv8bdm82e8v8ga3 - only a-z and/or 0-9
vp4xnbvvgu59ykz8 - only a-z and/or 0-9
9uei8zr8dx4kecna - only a-z and/or 0-9
zr7n23hhvmrt7qzf - only a-z and/or 0-9
idxrdpxrkzbzby9b - only a-z and/or 0-9
pexqgf7b3w59m4n6 - only a-z and/or 0-9
gjvt9sjtsut4s2dg - only a-z and/or 0-9
fb6vqc7r9ayucmyt - only a-z and/or 0-9
7tkek58cyzfrzs7f - only a-z and/or 0-9
3casnfiwpgq24pt9 - only a-z and/or 0-9
nWLheuvjCsZaMK4M - only a-z and/or 0-9
34ZZcf5qVYQhqNsd - only a-z and/or 0-9
sDMW7HGJ9qsU9uqB - only a-z and/or 0-9
wQiZ5nQZMpGCB9q3 - only a-z and/or 0-9
LBYSSniZcb4kVsvR - only a-z and/or 0-9
GezLTPunDAzPJpRd - only a-z and/or 0-9
zx6Skn2NP49tv4KC - only a-z and/or 0-9
gJX9gRwtawHskgWj - only a-z and/or 0-9
daCPNCkkFtdBvxMc - only a-z and/or 0-9
FtkwJhqtd7kPmG9R - only a-z and/or 0-9
97RRGD(&9SsPeUDv - only a-z and/or 0-9
SCbAgAxW=-5Ca]Tt - only a-z and/or 0-9
r}F5BL9Uw)}Z]BR* - only a-z and/or 0-9
[+JCKBsbL#z%bNwQ - only a-z and/or 0-9
M9bgcv9X}73ZTaf* - only a-z and/or 0-9
j(L2G=nmAiqx4hJ[ - only a-z and/or 0-9
|#D]xW5vkERA|4Jh - only a-z and/or 0-9
=nu]$cJCJ78Xz[4w - only a-z and/or 0-9
aRUW#fNn$x&~)NhE - only a-z and/or 0-9
9Zz?=EYbWp7RLHH$ - only a-z and/or 0-9
jNyw(-s&r+}3|)5G - only a-z and/or 0-9
}i*+{hUCVuW=GfA6 - only a-z and/or 0-9
kK%dQvnN$jV7+Tza - only a-z and/or 0-9
wyjM8ABmUb(7}EDn - only a-z and/or 0-9
7N95mBJTxMcgy!Pb - only a-z and/or 0-9
TdXmcw*CeixWy}7| - only a-z and/or 0-9
Hhvc2VpQ9y#i[ZU= - only a-z and/or 0-9
RaX4zB(7kQMir2]S - only a-z and/or 0-9
B3u)a=EX?(4t[LF8 - only a-z and/or 0-9
}T4?ZbRG+w){%gNF - only a-z and/or 0-9
# [green]awk '$1 ~ /^[a-z0-9]+$/{print $0" - only a-z and/or 0-9"}' input2[/green]
9pv8bdm82e8v8ga3 - only a-z and/or 0-9
vp4xnbvvgu59ykz8 - only a-z and/or 0-9
9uei8zr8dx4kecna - only a-z and/or 0-9
zr7n23hhvmrt7qzf - only a-z and/or 0-9
idxrdpxrkzbzby9b - only a-z and/or 0-9
pexqgf7b3w59m4n6 - only a-z and/or 0-9
gjvt9sjtsut4s2dg - only a-z and/or 0-9
fb6vqc7r9ayucmyt - only a-z and/or 0-9
7tkek58cyzfrzs7f - only a-z and/or 0-9
3casnfiwpgq24pt9 - only a-z and/or 0-9
# [blue]awk '$1 !~ /^[a-z0-9]+$/{u=1}{if(u!=1){print $0" - only a-z and/or 0-9"}else {print $0" - not only a-z and/or 0-9"}}' input2[/blue]
9pv8bdm82e8v8ga3 - only a-z and/or 0-9
vp4xnbvvgu59ykz8 - only a-z and/or 0-9
9uei8zr8dx4kecna - only a-z and/or 0-9
zr7n23hhvmrt7qzf - only a-z and/or 0-9
idxrdpxrkzbzby9b - only a-z and/or 0-9
pexqgf7b3w59m4n6 - only a-z and/or 0-9
gjvt9sjtsut4s2dg - only a-z and/or 0-9
fb6vqc7r9ayucmyt - only a-z and/or 0-9
7tkek58cyzfrzs7f - only a-z and/or 0-9
3casnfiwpgq24pt9 - only a-z and/or 0-9
nWLheuvjCsZaMK4M - not only a-z and/or 0-9
34ZZcf5qVYQhqNsd - not only a-z and/or 0-9
sDMW7HGJ9qsU9uqB - not only a-z and/or 0-9
wQiZ5nQZMpGCB9q3 - not only a-z and/or 0-9
LBYSSniZcb4kVsvR - not only a-z and/or 0-9
GezLTPunDAzPJpRd - not only a-z and/or 0-9
zx6Skn2NP49tv4KC - not only a-z and/or 0-9
gJX9gRwtawHskgWj - not only a-z and/or 0-9
daCPNCkkFtdBvxMc - not only a-z and/or 0-9
FtkwJhqtd7kPmG9R - not only a-z and/or 0-9
97RRGD(&9SsPeUDv - not only a-z and/or 0-9
SCbAgAxW=-5Ca]Tt - not only a-z and/or 0-9
r}F5BL9Uw)}Z]BR* - not only a-z and/or 0-9
[+JCKBsbL#z%bNwQ - not only a-z and/or 0-9
M9bgcv9X}73ZTaf* - not only a-z and/or 0-9
j(L2G=nmAiqx4hJ[ - not only a-z and/or 0-9
|#D]xW5vkERA|4Jh - not only a-z and/or 0-9
=nu]$cJCJ78Xz[4w - not only a-z and/or 0-9
aRUW#fNn$x&~)NhE - not only a-z and/or 0-9
9Zz?=EYbWp7RLHH$ - not only a-z and/or 0-9
jNyw(-s&r+}3|)5G - not only a-z and/or 0-9
}i*+{hUCVuW=GfA6 - not only a-z and/or 0-9
kK%dQvnN$jV7+Tza - not only a-z and/or 0-9
wyjM8ABmUb(7}EDn - not only a-z and/or 0-9
7N95mBJTxMcgy!Pb - not only a-z and/or 0-9
TdXmcw*CeixWy}7| - not only a-z and/or 0-9
Hhvc2VpQ9y#i[ZU= - not only a-z and/or 0-9
RaX4zB(7kQMir2]S - not only a-z and/or 0-9
B3u)a=EX?(4t[LF8 - not only a-z and/or 0-9
}T4?ZbRG+w){%gNF - not only a-z and/or 0-9
#
 
u should be reinitialized at each iteration.
But why even using a flag ?
Code:
awk '{if($1~/^[a-z0-9]+$/)print $0" - only a-z and/or 0-9";else print $0" - not only a-z and/or 0-9"}' input2

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top