Home
Virtual Image Printer DriverMiraplacid Publisher converts documents to images
Virtual Text Printer DriverMiraplacid Text Driver extracts text from documents
Miraplacid Data ViewerMiraplacid Data Viewer - access to internal structure of a binary file
Miraplacid Text ViewerMiraplacid Text Viewer - access to internal structure of a text file
Common:   Login | Rules of forum | Register
Management:  New topic | Close topic | Move topic | Delete topic
  

Miraplacid Forum >> Virtual Printer Drivers >> HELP! script to analyze a stringModerator:Cerberus

Author Topic:  HELP! script to analyze a string
Mark Woodman
Member

From: Taiwan
posted 2003-06-11 11:26:20 Reply -Delete

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 .........

moe
Member

From: USA
posted 2003-06-11 11:27:07 Reply -Delete

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?.

maloca
Member

From: USA
posted 2003-06-11 11:27:34 Reply -Delete

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

Something like that :)

mancubus
Member

From: Belgium
posted 2003-06-11 11:27:58 Reply -Delete

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.

Ola
Member

From: USA
posted 2003-06-11 11:29:57 Reply -Delete

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

lynn
Member

From: Germany
posted 2003-06-11 11:31:42 Reply -Delete

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.

Aux Dee
Member

From: France
posted 2003-06-11 11:32:03 Reply -Delete

echo $filename |
awk -F"\" '{
for ( i=1 ; i

moe
Member

From: USA
posted 2003-06-11 11:33:09 Reply -Delete

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

Aux Dee
Member

From: France
posted 2003-06-11 11:34:21 Reply -Delete

oops

that awk -F"\" should be
awk -F"/"

Aux Dee
Member

From: France
posted 2003-06-11 11:34:35 Reply -Delete

another typo

for ( i=1 ; i

Aux Dee
Member

From: France
posted 2003-06-11 11:34:49 Reply -Delete

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";
}'

Rob
Member

From: USA
posted 2003-06-11 11:35:03 Reply -Delete

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

Your message:
Name, password:
Go to category:  

Technical Support

Copyright © Miraplacid, 2003 - 2021