Computing.Net > Forums > Unix > cut problem

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.

cut problem

Reply to Message Icon

Name: abby
Date: July 18, 2002 at 15:17:55 Pacific
Comment:

Hi all,
I am trying to cut the last field in
myPath=/home/aby/asdf/newDir

I want to sprune it to
myDir=/home/aby/asdf
and assign "temp=newDir"

Wondering if cut can do it or is there a better way thru' awk.
Thanks,
abby



Sponsored Link
Ads by Google

Response Number 1
Name: Frank
Date: July 19, 2002 at 00:09:22 Pacific
Reply:

Hi Abby,

hope this helps:
echo "/home/aby/asdf/newDir" | awk 'FS="/" {print $(NF) }'

No RSIK no fun
Frank


0

Response Number 2
Name: Jerry Lemieux
Date: July 19, 2002 at 12:39:26 Pacific
Reply:

Try printing your PATH variable in this format:

temp=${PATH##*/}

This will set $temp to the value after the last /. Using awk for this purpose is overkill.

Jerry



0

Response Number 3
Name: James Boothe
Date: July 20, 2002 at 06:17:27 Pacific
Reply:

abby wants to isolate myPath into two segments. Adding to Jerry's approach:

myDir=${myPath%/*}
temp=${myPath##*/}

The unix commands dirname and basename are made just for this purpose:

myDir=`dirname $myPath`
temp=`basename $myPath`

However, these commands take more cpu time, so I prefer the built-in commands above, assuming that you can use ksh.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







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: cut problem

test generation problems www.computing.net/answers/unix/test-generation-problems/6693.html

comparison problem www.computing.net/answers/unix/comparison-problem/2240.html

UNIX cut & grep specific columns www.computing.net/answers/unix/unix-cut-amp-grep-specific-columns/5201.html