1753784 Members
7129 Online
108799 Solutions
New Discussion юеВ

awk help required.

 
SOLVED
Go to solution
James R. Ferguson
Acclaimed Contributor

Re: awk help required.

HI (again):

> Now Iam looking for an statement that Incase particular entry (red...) doesn't exists in dynamic log file then write into other log file.

I presume that you want the list of filenames in which the token doesn't appear. One simple way is:

# grep -v red * /dev/null|awk -F: '{print $1}'|sort -u > myresults

...which looks at all files in your current directory that do *not* contain the token "red". Lines in each file that fail to match are output with the filename and a colon (":") separator before the line content. We snip out this name and reduce the population to a unique list.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: awk help required.

>JRF: I presume that you want the list of filenames in which the token doesn't appear.
# grep -v red * /dev/null | awk -F: '{print $1}' | sort -u

This isn't going to do that. It's going to list all files that have lines that don't have red.
You need something like:
for file in *; do
grep -q -w red $file
if [ $? -ne 0 ]; then
echo "$file" # no scummy red, nowhere
fi
done > ../myresults

>fail to match are output with the filename and a colon

-l already does that.

If you don't want to invoke grep in a loop, you can use vector methods and do grep -l all all files then use comm(3) with ls(1) to weed out the the reds.
James R. Ferguson
Acclaimed Contributor

Re: awk help required.

Hi (again):

> Dennis: >JRF: I presume that you want the list of filenames in which the token doesn't appear...This isn't going to do that.

Yes, you are correct of course. I'm not sure what I was thinking and my quick test was a shambles of rigor :-)

A one-liner might be"

# grep -c -w red *|awk -F: '$2==0 {print $1}'

...which would show the filenames *not* containing the "word" 'red'.

Regards!

...JRF...