Extract filename from path in csh shell - from file list

How to extract the filename from the path; I have a list of files. I am using csh shell and install awk, sed, perl.

/ dfgfd / dfgdfg / filename

gotta give me

file name

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.

THH

+2


source to share


4 answers


The standard program basename

does what you want:



$ basename /dfgfd/dfgdfg/filename
filename

      

+9


source


This way worked around for me. You said you have perl, so this should run. It replaces all opaque texts up to the last / with nothing (effectively removing it).



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

      

+1


source


Ok, I found a good solution using AWK:

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

      

0


source


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

      

try it!

as a base parameter accepet accepet as an argument, so we are forcing xargs to split it one by one.

0


source







All Articles