Filter files with specific file extensions using regular expression
I have a file containing a list of filenames:
esocket.c
esocket.h dockwin.cpp dockwin.h makefile getblob . etc...
I am looking for a regular expr开发者_如何学JAVAession (preferably Unix syntax) to do the following:
- get lines that have .c, cpp and .h files
- get lines that don't have a file extension.
egrep '^[^.]*(\.(cpp|c|h))?$' yourfile
gawk
awk '
{
for(i=1;i<=NF;i++){
if ( $i ~ /\.(c|h|cpp)$/){
print "file with extension: "$i
}else{
print "file w/o extension: "$i
}
}
}' file
output
$ ./shell.sh
file with extension: esocket.c
file with extension: esocket.h
file with extension: dockwin.cpp
file with extension: dockwin.h
file w/o extension: makefile
file w/o extension: getblob
精彩评论