views:

1068

answers:

3

How to extract filename from the path; i have a list of files. I'm using csh shell, and have awk, sed, perl installed.

/dfgfd/dfgdfg/filename

should give me

filename

I tried basename:

    find $PROJDIR -name '*.c' -o -name '*.cc' -o -name '*.h'
 | xargs grep -l pattern | xargs basename

and it gave me the following error:

basename: too few arguments Try `basename --help' for more information.

thx

+2  A: 

The standard program basename does what you want:

$ basename /dfgfd/dfgdfg/filename
filename
Thomas
my issue is slightly different, and basename won't work. check the edited question.
vehomzzz
`basename` only works on a single filename at a time. Replace the last part by `xargs -n1 basename`.
Thomas
Also try `-r`, to avoid running a `basename` with no arguments if there are no matches.
ephemient
+1  A: 

This kind of workaround worked for me. You said you had perl so this should run. It replaces all nonspace text up to the last / with nothing (effectively deleting it).

find $PROJDIR -name '*.c' -o -name '*.cc' -o -name '*.h'
 | xargs grep -l pattern | perl -pi -e "s/\S+\///g"
FModa3
A: 

Ok I found a nice solution using AWK:

    find  $PROJECT_DIR  -name '*.c' -o -name '*.cc' -o -name '*.h' 
| xargs grep -l PATTERN | awk -F "/" '{print $NF}'
vehomzzz