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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to check? 2

Status
Not open for further replies.

ogniemi

Technical User
Nov 7, 2003
1,041
PL
expected/valid strings are:

home=/u/user1
home=/u/user2/


Not valid are:

home=/u/user3/blabla
home=/u/user4/bla/bla
home=/user5
home=/


How to ensure (test) that the string is right/expected one?

lets say the string is on $5. I did the following test:

[ `echo $5|grep ^home\=\/u\/.|wc -l` -eq 1 ]


But it doesn't aware for following - unexpected:

home=/u/user3/blabla
home=/u/user4/bla/bla
 
am I doing something wrong?

using your test everything is not valid:

# echo "home=/u/"|grep -q '^home=/u/[^/]*/\?$' && echo "Yes, is valid" || echo not
not
# echo "home=/u"|grep -q '^home=/u/[^/]*/\?$' && echo "Yes, is valid" || echo not
not
# echo "home=/u/uuu"|grep -q '^home=/u/[^/]*/\?$' && echo "Yes, is valid" || echo not
not
# ( echo "home=/u/uuu"|grep -q '^home=/u/[^/]*/\?$' ) && echo "Yes, is valid" || echo not
not
# ( echo "home=/u/uuu/"|grep -q '^home=/u/[^/]*/\?$' ) && echo "Yes, is valid" || echo not
not
# ( echo "home=/u/uuu/fsfsf"|grep -q '^home=/u/[^/]*/\?$' ) && echo "Yes, is valid" || echo not
not
#
 
Hi

From your sample only the second ( /u ) and the last ( /u/uuu/fsfsf ) was evaluated to "not" by [tt]bash[/tt] 2.05b and [tt]pdksh[/tt] 5.2.14 using GNU [tt]grep[/tt] 2.5.1.

Feherke.
 
ok, I am working with ksh - that's why it doesn't work.

any idea/proposal for ksh?
 
and i am working on AIX - not using GNU grep.
 
Hi

Similar code for [tt]sed[/tt]. Mine is GNU [tt]sed[/tt] 4.1.2, could not work on other implementations.
Code:
echo "$5" | sed -n '/^home=\/u\/[^\/]*\/\?$/aYes, is valid'

Feherke.
 
Hi

This works with [tt]gawk[/tt] 3.1.1 and [tt]mawk[/tt] 1.3.3, so may be compatible with whatever [tt]awk[/tt] you have.
Code:
echo "$5" | awk '/^home=\/u\/[^\/]*\/?$/{print "Yes, is valid"}'

Feherke.
 
Here's something along the lines of what you were originally trying...
Code:
(( $(print "$5" | sed 's|^home=/u/||;s|/| |g' | wc -w) - 1 )) && print Bad || print Good
Hope this helps.
 
thx.

I would expect below 6 is not valid and 7, 13, 14 , 15, 16 valid - matched as "Yes, is valid"

thx in advance,
r,m.

# C=1
# for i in / // /// /u /u/ /u/u /u/u// /u/u/u/ u/u/u u/u//u u//u/u/ /u/u//u/// /u/u// /u/u/// //u/u// //u///uuu;do echo "$C\thome=$i $(echo home=$i|awk '/^home=\/u\/[^\/]*\/?$/{print "Yes, is valid"}')";let "C = C + 1";done
1 home=/
2 home=//
3 home=///
4 home=/u
5 home=/u/ Yes, is valid
6 home=/u/u Yes, is valid
7 home=/u/u//
8 home=/u/u/u/
9 home=u/u/u
10 home=u/u//u
11 home=u//u/u/
12 home=/u/u//u///
13 home=/u/u//
14 home=/u/u///
15 home=//u/u//
16 home=//u///uuu
 
sorry, not 6 but 5 is expected to be not valid. 6 is OK of course.
 
A plain ksh solution:

Code:
if [[ "$home" == /u/user+([0-9])?(/) ]]
then
    echo "it's good"
else
    echo "it's not"
fi

Annihilannic.
 
Hi

I see you got your star, Annihilannic, I suppose your solution works fine.
So [tt]pdksh[/tt] is not enough good to substitute [tt]ksh[/tt]. I spent some time yesterday trying something similar, but no way to get it work as expected.

Feherke.
 
Works fine for me in pdksh-5.2.14?

[tt]$ for home in /u/user3 /u/user123 /user53 /u/user3/ /u/user3/blah
> do
> if [[ "$home" == /u/user+([0-9])?(/) ]]
> then
> echo "$home is good"
> else
> echo "$home is not"
> fi
> done
/u/user3 is good
/u/user123 is good
/user53 is not
/u/user3/ is good
/u/user3/blah is not
$ rpm -qf /bin/ksh
pdksh-5.2.14-22
$[/tt]

Make sure you haven't quoted the second string (because it needs to use ksh filename expansion semantics, which quotes prevent), and that you are using the built-in ksh [[ test operator.

Annihilannic.
 
Hi

Annihilannic said:
Make sure you haven't quoted the second string (...) and that you are using the built-in ksh [[ test operator.
Hey, I am just stupid, not beginner. :) Of course I did not quoted it, and put the expression between double brackets.

The difference was that I tried to match the second name in then path ( the user00 ) as not slash ( like in my above [tt]grep[/tt], [tt]expr[/tt], [tt]sed[/tt] and [tt]awk[/tt] codes ) with [tt]!(...)[/tt]. Of course, your solution with character interval works fine. [medal]

By the way, works in [tt]bash[/tt] too.

Feherke.
 
If the file you were checking was called data.txt, you could use perl like this:

perl -ne 'print if /home=\/+u\/+[a-z0-9]+(\/*)$/' data.txt
 
For a good/bad result:

perl -ne 'if(/home=\/+u\/+[a-z0-9]+(\/*)$/)
{print "$_ is good\n"}
else
{print "$_is bad\n"}' data.txt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top