tags:

views:

25

answers:

2

hi friend's

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 WORD in file except lines that begin with //

Remark: "//" not necessary exists in the first of the line it's could be some spaces after the first of the line and then appears //

for example

more file

 // WORD

 AA WORD

         // ss WORD

lidia

A: 
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.

Johnsyweb
A: 
awk '!/^[ \t]*\/\// && /WORD/{m=gsub("WORD","");total+=m}END{print total}' file
ghostdog74
how to count the WORD with your example (if I want to know how many WORD i Have in file)
lidia
@lidia: How to *count* the number of WORDs in a file is a separate question. You should either update your question or ask a new one.
Johnsyweb