0

I have some files in different route like below:

./a/b/c/test.h
./d/e/f/g/h/abc1.c
./i/j/k/l/m/n/o/p/hello.log
...

And I want to get only the file name of each one. That is: I want to get "test.h", "abc1.c", "hello.log".

As the deep of the route is uncertain, so maybe awk can't help my problem. Can anyone help me with this?

4

3 回答 3

2

You can use the basename command:

while read line
do
basename $line
done < "myfile"
于 2012-05-10T06:44:40.960 回答
0

basename通常是这样做的方法,但在 AWK 中你可以这样做:

filename=./i/j/k/l/m/n/o/p/hello.log
echo "$filename" | awk -F/ '{print $NF}'

其他:

awk -v filename="$filename" 'BEGIN {len = split("/", filename, a); print a[len]}'

在纯 Bash 中:

echo ${filename##*/}
于 2012-05-10T08:53:40.917 回答
0
awk -F"/" '{print $NF}' file_name
于 2012-05-10T08:57:33.347 回答