How to do String manipulations using Substring function in Shell

Hi,

I have a scenario to just plug out the file name from the following location path.

/opt/project/data/int/holdFiles/csv195687.csv

So, how do I get just file name which is "csv195687.csv" from the above line using awk/shell scripting? Can we use indexOf and Substring in awk to get last indexOf "/" and then end of String to get file name?

Thanks.

if u made a little search, you could have get what u wanted by urself....

 
str="/opt/project/data/int/holdFiles/csv195687.csv"
echo ${str##*/}
csv195687.csv

Thanks for quick reply. It is a cool answer. From next time onwards, I will definitely search the forum before I ask the question.

$
$ str="/opt/project/data/int/holdFiles/csv195687.csv"
$ basename $str
csv195687.csv
$
$ echo $str | sed 's/.*\///'
csv195687.csv
$
$ echo $str | awk -F/ '{print $NF}'
csv195687.csv
$
$ echo $str | perl -lne '/.*\/(.*)/ && print $1'
csv195687.csv
$

tyler_durden

Thanks Tyler. Hmmm...lot of options.

str="/opt/project/data/int/holdFiles/csv195687.csv"
basename $str

echo "/opt/project/data/int/holdFiles/csv195687.csv" |awk -F[/] '{print $NF}' 

The only one that makes sense for dealing with a string is:

echo ${str##*/}

Save external commands for dealing with files.