Computing.Net > Forums > Unix > how to parse a path in unix

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

how to parse a path in unix

Reply to Message Icon

Name: NeedHelp
Date: June 7, 2002 at 06:38:12 Pacific
Comment:

Hello,

If i have the following in a unix script:

echo "Enter the full path: \c"
read PATH

The user will input something like:

/mike/hobbies/football

How can I now take the variable PATH that the user inputted with the full path and parse it so I can save each directory path to a seperate variable, for example I will want to accomplish this in my script:

Path1=mike
Path2=hobbies
Path3=football

Where Path1, Path2, Path3 are unix variables I want the user input to be saved. Basically how do I parse it and not store the /

Thanks



Sponsored Link
Ads by Google

Response Number 1
Name: LANkrypt0
Date: June 7, 2002 at 07:02:53 Pacific
Reply:

tr and awk should do the trick, so something like this:

Path1=`echo $PATH | tr "[/]" "[ ]" | awk '{print $1}'`
Path2=`echo $PATH | tr "[/]" "[ ]" | awk '{print $2}'`
Path3=`echo $PATH | tr "[/]" "[ ]" | awk '{print $3}'`

So you are replacing / with an empty space and then using awk to specify which field you want to read.


0

Response Number 2
Name: James Boothe
Date: June 7, 2002 at 07:58:12 Pacific
Reply:

or after changing slashes to spaces, you could just pipe it into a read command:

echo $PATH |
tr "[/]" "[ ]" |
read Path1 Path2 Path3 remainder


0

Response Number 3
Name: LANkrypt0
Date: June 7, 2002 at 08:16:46 Pacific
Reply:

I would suggest that, but my shell provider runs an old version of KSH so that pipe to read does not work :)


0

Response Number 4
Name: James Boothe
Date: June 7, 2002 at 13:53:42 Pacific
Reply:

Bummer! I use that a lot. Also, NeedHelp did not say if the number of slashes in the path can vary. If so, the following code would assign the segments to path1, path2, path3, etc.

n=0
for word in `echo $mypath | tr "[/]" "[ ]"`
do
n=`expr $n + 1`
eval path$n=$word
done


0

Response Number 5
Name: LANkrypt0
Date: June 7, 2002 at 14:00:58 Pacific
Reply:

I wish I could, oh well. I just make do with what I have.


0

Related Posts

See More



Response Number 6
Name: Sean Miller
Date: June 13, 2002 at 03:09:26 Pacific
Reply:

Hows about this...

path="/usr/spool/lp/interface"
var1=`echo $path | awk -F'/' '{ print $2 }'`
var2=`echo $path | awk -F'/' '{ print $3 }'`
var3=`echo $path | awk -F'/' '{ print $4 }'`

???

Obviously $1 will be the bit before the first / so will contain nothing.

Sean


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: how to parse a path in unix

How to parse a file of 199 col? www.computing.net/answers/unix/how-to-parse-a-file-of-199-col/7250.html

how to pass a variable from unix www.computing.net/answers/unix/how-to-pass-a-variable-from-unix-/7649.html

how to list a directory in realtime www.computing.net/answers/unix/how-to-list-a-directory-in-realtime/8218.html