开发者

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:

  1. get lines that have .c, cpp and .h files
  2. 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜