开发者

grep or sed or awk + match WORD

开发者 https://www.devze.com 2023-01-14 19:36 出处:网络
I do the following in order to get all WORD in file but not in lines that start with "//" grep -v "//"file | grep WORD

I do the following in order to get all WORD in file but not in lines that start with "//"

  grep -v "//"  file | grep WORD

Can I get some other elegant suggestion to find all occurrences of WORD in the file except lines that begin with //?

Remark:开发者_StackOverflow "//" does not necessarily exist at the beginning of the line; there could be some spaces before "//".

For example

 // WORD
 AA WORD
         // ss WORD


grep -v "//"  file | grep WORD

This will also exclude any lines with "//" after WORD, such as:

WORD // This line here

A better approach with GNU Grep would be:

 grep -v '^[[:space:]]*//' file | grep 'WORD'

...which would first filter out any lines beginning with zero-or-more spaces and a comment string.

Trying to put these two conditions into a single regular expression is probably not more elegant.


awk '!/^[ \t]*\/\// && /WORD/{m=gsub("WORD","");total+=m}END{print total}' file
0

精彩评论

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