awk to get result of multiple lines in one sentence with if statement

Viewed 181

I am new to awk and I was wondering if I could get one single result for an if operation on awk.

Example:

cat example.txt:

0
0
0
0
0

awk '{ if ($1==0) print "all zeros"; else print "there is 1"}'

result:

all zeros
all zeros
all zeros
all zeros
all zeros

I would like to have only one all zeros as answer or a TRUE . Is this the case where I should use an awk function to return something ? Thanks

4 Answers

Have your code in this way. Written and tested with shown samples.

awk '$0==0{count++} END{if(count==FNR){print "TRUE"}}' Input_file

OR

awk '$0==0{count++} END{if(count==FNR){print "All lines are zeroes"}}' Input_file

OR to print a message when some non-zero line(s) found:

awk '$0==0{count++} END{if(count==FNR){print "TRUE"} else{print "There is  non-zero line(s) found."}}' Input_file

Explanation: Adding detailed explanation for above.

awk '               ##Starting awk program from here.
$0==0{              ##Checking condition if current line is zero then do following.
  count++           ##Increasing count with 1 here.
}
END{                ##Starting END block of this program from here.
  if(count==FNR){   ##Checking condition if count is equal to number of total lines of file.
    print "TRUE"    ##If above condition is TRUE then print TRUE here.
  }
}
' Input_file        ##Mentioning Input_file name here.

Here is an alternative using gnu-awk:

awk -v RS='^(0\r?\n)+$' '{print (NF ? "there is 1" : "all zeros")}' file

all zeros

I would do it following way using GNU AWK let file.txt content be

0
0
0
0
0

then

awk '{nonzero = nonzero || $1!="0"}END{print nonzero?"has not zero":"all zeros"}' file.txt

output

all zeros

Explanation: I am using nonzero to store information if some non-zero was already save (value 1) or not (value 0). If you are about variable which is not set in awk in arithmetic context then its value is 0, so I do not need to declare nonzero=0 in BEGIN section. I harness || which is logical or and might be described as follows:

  • if you did not see non-zero earlier and do not see it now that mean there is not non-zero element so far (0 || 0 == 0)
  • if you did not see non-zero earlier and do see it now that mean there is non-zero element so far (0 || 1 == 1)
  • if you did see non-zero earlier and do not see it now that mean there is non-zero element so far (1 || 0 == 1)
  • if you did see non-zero eralier and do see it now that mean there is non-zero element so far (1 || 1 == 1)

After processing all lines, in END section I print either has not zero or all zeros depending on nonzero value and harnessing ternary.

(tested in gawk 4.2.1)

Another:

$ awk '$0{exit v=1}END{printf "All %szeroes\n",(v?"not ":"")}' file

Output with sample data:

All zeroes

Alternative output:

All not zeroes

Explained:

$ awk '
$0 {                                       # if record evaluates to non-zero
    exit v=1                               # jump to END with "parameter" 1
}                                          # why continue once non-zero seen
END {
    printf "All %szeroes\n",(v?"not ":"")  # if "parameter" v was set, output "not"
}' file

The condition to examine $0 could of course be something more specific (like $0=="0") but it's sufficient for this purpose. exit v=1 sets var v to value 1 but it also exits the program once a non-zero value of $0 has been found and jumps to END where the value of v is examined. The program finally exits with exit code 1. If that is not acceptable, you need to exit from END explicitly with exit 0.

Related