It is currently Thu, 20 Jan 2022 09:35:12 GMT



 
Author Message
 Parsing a string with KSH
Given the following string a=/one/two/three/four/five

Is there a statement I can use (could be sed, awk ... ) that will let
me substitute a numeric value to return one of the following values:

/one
/one/two
/one/two/three

Or some other method other than using multiple dirname's.

An example would be very much appreciated.

Thanks in advance to all that reply.



 Sun, 10 Sep 2006 00:53:02 GMT   
 Parsing a string with KSH

awk -F"/" '{print $2}

will print "one". $3 will be "two", etc. If you strip off the leading "/" or
just subtract 1, the digits will match the textual numbers.

        Ed.



 Sun, 10 Sep 2006 01:05:34 GMT   
 Parsing a string with KSH

echo "$a" | cut -d/ -f1-3

   Or:

IFS=/
set -- $a
echo /$2/$3

   Or any of several other methods.

--
    Chris F.A. Johnson                  http://cfaj.freeshell.org/shell
    ===================================================================
    My code (if any) in this post is copyright 2004, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License



 Sun, 10 Sep 2006 01:37:25 GMT   
 Parsing a string with KSH

sed -e 's:^\(/[^/][^/]*\)\{N\}:&\
:;P;d
'

substitute N in above to retrieve corresponding values.



 Sun, 10 Sep 2006 12:32:38 GMT   
 Parsing a string with KSH

I went with this implementation. It seems to suit me the best

echo ${a%*/*/*/*}
echo ${a%*/*/*}

Thanks for your help



 Sun, 10 Sep 2006 21:26:38 GMT   
 Parsing a string with KSH

I went with this implementation. It seems to suit me the best

echo ${a%*/*/*/*}
echo ${a%*/*/*}

Thanks for your help



 Sun, 10 Sep 2006 21:26:38 GMT   
 
   [ 6 post ] 

Similar Threads

1. KSH challenge: string parsing to a perfect PS1

2. Parse errors compiling string program using SGI sTL with g++

3. Can Flex Parse Multiple _Strings_?

4. string parse

5. Quick question on parsing a string!

6. String parsing

7. sscanf string parsing

8. Parsing character strings-(not FILE *) with lex

9. How can I parse a string in csh?


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software