Operating System - HP-UX
1834276 Members
2811 Online
110066 Solutions
New Discussion

Re: HELP! script to analyze a string

 
SOLVED
Go to solution
Enrico Venturi
Super Advisor

HELP! script to analyze a string

Hello colleagues,
I need some help...
I'm writing a script in Korn shell and I should parse a string contained in a file; the string is a file name, it can contain wildcards; I want parse the string to discover if the wildcards are before the last "/" in the name, i.e. if
/usr/1353sh/*/conf/
or /usr/1353sh/eml/*
How can I parse the string? I don't know the awk .........
13 REPLIES 13
Sridhar Bhaskarla
Honored Contributor

Re: HELP! script to analyze a string

Hi,

The file 'list' has the following lines

$cat list
/usr/112/*/conf
/usr/askdj/eml/*
/usr/askjk*/ems/
/usr/askjk/ems/
*/usr/askjk*/ems/

$awk '/\/\*/ {print}' list
/usr/112/*/conf
/usr/askdj/eml/*



Is this what you wanted?.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Zeev Schultz
Honored Contributor

Re: HELP! script to analyze a string

can use grep,like
echo "/usr/1353sh/*/conf" | grep "\/\*\/[a-zA-Z]"

Something like that :)

Zeev
So computers don't think yet. At least not chess computers. - Seymour Cray
Enrico Venturi
Super Advisor

Re: HELP! script to analyze a string

Not exactly:
I have a variable (inside my script) which represents a string read from a file; the variable (the string) represents the name of a file, of a directory, of a set of files or directory;
example:
$file = /usr/1353sh/AS/conf/*
or $file = /usr/1353sh/AS/conf/?aram.cfg
or $file = /usr/1353sh/AS/conf/[a-zA-Z]21.cfg
i.e. the name can contain wildcards;
I need to discover if the wildcards are at the end of the file name or are in the middle (i.e. they refer to directories)
e.g. /usr/1353sh/AS/*/param.cfg

Therefore I have to scan the string and to look for the wildcards, if the wildcards are before the "/" then I suppose that they refer to directory, not to the file name.
Hai Nguyen_1
Honored Contributor

Re: HELP! script to analyze a string


echo $STRING | grep -q '\*$'

if [ $? -eq 0 ];
then
echo '* is at the end of the string'
else
echo '* is in the middle of the string'
fi

Hai
Jean-Louis Phelix
Honored Contributor

Re: HELP! script to analyze a string

hi,

I'm not sure that knowing if there is a wildcard of any kind somewhere in the string would be easy (or useful ?). There are other wildcards apart of * ...

Perhaps you could rather expand the string and get the real pathes using for example :

set -A LIST $file

then ${#LIST[*]} would give you the number of pathes, each array element being addressed using ${[LIST[x]), ie :

#!/usr/bin/sh

A=/usr/bin/li*

set -A LIST $A

i=${#LIST[*]}
j=0
while [ $j -lt $i ]
do
echo $j ${LST[j]} # or do anything else ...
j=$((j+1))
done

Regards.


It works for me (© Bill McNAMARA ...)
curt larson_1
Honored Contributor

Re: HELP! script to analyze a string

echo $filename |
awk -F"\" '{
for ( i=1 ; iif ( ( $i ~ "[\*\?\.\[]" ) {
if ( i == NF ){
print "wildcard is last field";exit;}
else {
print "wildcard is not the last field";exit;}
}}
print "no wildcard";
}'
Sridhar Bhaskarla
Honored Contributor

Re: HELP! script to analyze a string

Hi,

The trick is in declaring the variable.

set -f ARG
ARG='/usr/bin/*'
echo $ARG |grep "\/\*$" > /dev/null 2>&1
if [ $? = 0 ]
then
echo $ARG is a directory
fi

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
curt larson_1
Honored Contributor

Re: HELP! script to analyze a string

oops

that awk -F"\" should be
awk -F"/"
curt larson_1
Honored Contributor

Re: HELP! script to analyze a string

another typo

for ( i=1 ; i
should be

for ( i=1 ; i<=NF; i++ ) {

less then or equal
curt larson_1
Honored Contributor
Solution

Re: HELP! script to analyze a string

i noticed another typo

so i'm going to do it again, without the typos

echo $filename |
awk -F"/" '{
for ( i=1 ; i<=NF; i++ ) {
if ( $i ~ "[\*\?\.\[]" ) {
if ( i == NF ){
print "wildcard is last field";exit;}
else {
print "wildcard is not the last field";exit;}
}}
print "no wildcard";
}'
Rodney Hills
Honored Contributor

Re: HELP! script to analyze a string

Here is something that should work. It use a shell capability of pattern matching using ${var%%pattn}.

file1="/usr/1353sh/*/conf"
x=${file1#*[\[?*]}
if [ "$x" = "$file1" ] ; then
echo "No wildcards in $file1"
exit
fi
y=${x%/*}
if [ "$x" = "$y" ] ; then
echo "wildcard in filename"
else
echo "wildcard in directory"
fi

x will be set to the text after the first wildcard character. In this case "*", so x will be "/conf".

If variable x contains a slash then the text string has a wildcard before a directory.

Set y to any text within x that has a slash followed by any characters. If x and y match, then no slash and therefore wildcard is in filename.

HTH

-- Rod Hills
There be dragons...
vasundhara
Frequent Advisor

Re: HELP! script to analyze a string

Hi,

If ksh is not mandatory, you can do it with perl so easily I think...

pattern = [list of wild characters...]i.e.

pattern = [\*\.-]
if ($file !~ /pattern/)
{
print "No wild Characters\n";
}
else
{
print "Wild Characters are present\n";
}

Regards
VJ.
H.Merijn Brand (procura
Honored Contributor

Re: HELP! script to analyze a string

Hey, what perl are you using? I don't know what version supports that syntax:

a5 108 > cat xx.pl
pattern = [\*\.-]
if ($file !~ /pattern/)
{
print "No wild Characters\n";
}
else
{
print "Wild Characters are present\n";
}
a5 109 > perl -Mstrict -w xx.pl
syntax error at xx.pl line 1, near "-]"
Global symbol "$file" requires explicit package name at xx.pl line 2.
Execution of xx.pl aborted due to compilation errors.
Exit 255
a5 110 >

a5 115 > perl -le'print$ARGV[0]=~/[[*?]/?"has wild":"safe"' '/usr/1353sh/*/conf/'
has wild
a5 116 >

But be aware that passing arguments to whatever proces is likely to *NOT* contain wildcards, since shells expand arguments *before* passing them to subprocesses. So either quote them (as in above example) or write the complete script in one language (perl). Then still, you are never sure that passed arguments are not expanded by the shell before you are able to deal with them.

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn