Skip to content
Advertisement

Using Awk to print the unavailable record only once

I am writing an awk script which looks like this inside:

JavaScript

This is my file.

JavaScript

This tries to print the else if method once, but instead, it prints for all of the lines that do not include the searched value. If I entered 20, my output looks like this:

JavaScript

I have to use the awk command to print else if output once. Let’s say if I entered 700. How do I fix it to get this:

JavaScript

Advertisement

Answer

Perhaps a solution using the END clause? You can set a variable if you find a match, and then use an if in the END to print the message only if no matches are found.

JavaScript

Also I’m not sure you really want to be using printf… plain old print seems a better fit here. And $0 is a little easier than listing out $1, $2, $3, $4.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement