Computing.Net > Forums > Unix > Cut Command

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 Command

Reply to Message Icon

Name: newby
Date: July 5, 2003 at 13:35:20 Pacific
OS: Solaris 2.6
CPU/Ram: Unix server
Comment:

I have a file whose contents are

dir1/dir2/dir3/file1
dir1/dir2/dir3/file2
dir1/dir2/file3
dir1/dir2/file4
etc.

How can I use 'cut' command to get file 1, file2, file3, file4 and redirect it to a new file.
i.e., how can I use 'cut' command to supress all the characters before '/' and send file1,file2,file3 and file4 to another file.




Sponsored Link
Ads by Google

Response Number 1
Name: WilliamRobertson
Date: July 5, 2003 at 17:01:36 Pacific
Reply:

Try

sed 's#..*/##' sourcefile.dat > otherfile.dat


0

Response Number 2
Name: nails
Date: July 6, 2003 at 00:01:39 Pacific
Reply:

#Hi

#!/bin/ksh
# on Solaris 7

var="dir1/dir2/dir3/file1"

# first, use the basename command:

basename $var

# second, if you have ksh/bash, use the pattern matching operators:

echo ${var##*/}

# third, cut isn't the best tool for this job. awk is, but if you
# insist on using cut, determine the number of fields, i.e. count the number of slashes:

# set expands the positional parameters $1,$2, $3, etc
set - $(IFS="/"; echo $var)
ff=$# # number of fields

echo $var|cut -d "/" -f $ff

Regards,

Nails


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 Command

Help Requested - cut & sed cmds www.computing.net/answers/unix/help-requested-cut-amp-sed-cmds/5224.html

cut statment www.computing.net/answers/unix/cut-statment/5757.html

How to use AWK command www.computing.net/answers/unix/how-to-use-awk-command/6686.html