Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get the file name from the path

Tags:

shell

unix

awk

cut

I have a file file.txt having the following structure:-

./a/b/c/sdsd.c
./sdf/sdf/wer/saf/poi.c
./asd/wer/asdf/kljl.c
./wer/asdfo/wer/asf/asdf/hj.c

How can I get only the c file names from the path. i.e., my output will be

sdsd.c
poi.c
kljl.c
hj.c
like image 301
alankrita Avatar asked Dec 21 '22 13:12

alankrita


1 Answers

You can do this simpy with using awk.

set field seperator FS="/" and $NF will print the last field of every record.

awk 'BEGIN{FS="/"} {print $NF}' file.txt

or

awk -F/ '{print $NF}' file.txt

Or, you can do with cut and unix command rev like this

rev file.txt | cut -d '/' -f1 | rev

like image 81
avinashse Avatar answered Dec 23 '22 01:12

avinashse