Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.

#Hi
#!/bin/ksh
# on Solaris 7var="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 fieldsecho $var|cut -d "/" -f $ff
Regards,
Nails

![]() |
![]() |
![]() |

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