Operating System - HP-UX
1833304 Members
3007 Online
110051 Solutions
New Discussion

Re: Get the last delimited field in shell script !

 
SOLVED
Go to solution
Chris Fung
Frequent Advisor

Get the last delimited field in shell script !

Hi All,

I got some difficulties in getting the last delimited field in a shell script. I have tried "cut", "tr" and "awk"...but it doesn't seem to work.

E.g I got a file and each line of the file is a full path name of a file. Since each path will have different levels of subdirectories... It seems I can't simply get the file_name straight away !!

Consider the following content of the configuration file:

/a/b/c/d/test1.log
/f/g/h/test2.log
/x/y/test3.log

I would like to get all of the filenames out of the configuration file....therefore:

test1.log
test2.log
test3.log

Appreciated for you inputs.

Best Regards,

Chris,
3 REPLIES 3
john korterman
Honored Contributor
Solution

Re: Get the last delimited field in shell script !

Hi,
# basename /a/b/c/d/test1.log

or in a script

#!/usr/bin/sh
while read line
do
basename "$line"
done < ./configfile

regards,
John K.
it would be nice if you always got a second chance
Chris Fung
Frequent Advisor

Re: Get the last delimited field in shell script !

Oh...thanks a lot John !!

Cheers,

Chris
curt larson_1
Honored Contributor

Re: Get the last delimited field in shell script !

a couple of more ways

awk -F"/" '{print $NF;}' yourfile

sed 's/.*\///' yourfile

while read line
print ${line##*/}
done