- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: HELP! script to analyze a string
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2003 05:41 AM
06-10-2003 05:41 AM
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 .........
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2003 05:49 AM
06-10-2003 05:49 AM
Re: HELP! script to analyze a string
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2003 05:54 AM
06-10-2003 05:54 AM
Re: HELP! script to analyze a string
echo "/usr/1353sh/*/conf" | grep "\/\*\/[a-zA-Z]"
Something like that :)
Zeev
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2003 06:00 AM
06-10-2003 06:00 AM
Re: HELP! script to analyze a string
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2003 06:06 AM
06-10-2003 06:06 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2003 06:18 AM
06-10-2003 06:18 AM
Re: HELP! script to analyze a string
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2003 06:31 AM
06-10-2003 06:31 AM
Re: HELP! script to analyze a string
awk -F"\" '{
for ( i=1 ; i
if ( i == NF ){
print "wildcard is last field";exit;}
else {
print "wildcard is not the last field";exit;}
}}
print "no wildcard";
}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2003 06:32 AM
06-10-2003 06:32 AM
Re: HELP! script to analyze a string
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2003 06:33 AM
06-10-2003 06:33 AM
Re: HELP! script to analyze a string
that awk -F"\" should be
awk -F"/"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2003 06:35 AM
06-10-2003 06:35 AM
Re: HELP! script to analyze a string
for ( i=1 ; i
should be
for ( i=1 ; i<=NF; i++ ) {
less then or equal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2003 06:40 AM
06-10-2003 06:40 AM
Solutionso 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";
}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2003 06:40 AM
06-10-2003 06:40 AM
Re: HELP! script to analyze a string
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2003 03:20 AM
06-11-2003 03:20 AM
Re: HELP! script to analyze a string
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2003 03:48 AM
06-11-2003 03:48 AM
Re: HELP! script to analyze a string
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