Reputation: 27
read n
awk '
BEGIN {sum=0;}{if( $0%2==0 ){sum+=$0;
}
}
END { print sum}'
Here i add, sum of even numbers
and what i want is, initially i give input as how many(count) and then the numbers i wanted to check as even and add it.
eg)
3
6
7
8
output is : 14
here 3 is count
and followed by numbers i want to check
, the code is executed correctly and output is correct, but i wanted to know how $0 left the count value i.e) 3
and calculates the remaining numbers.
Upvotes: 0
Views: 849
Reputation: 67507
you're reading the count but not using it in the script, a rewrite can be
$ awk 'NR==1 {n=$1; next} // read the first value and skip the rest
!($1%2) {sum+=$1} // add up even numbers
NR>n {print sum; exit}' file // done when the # linespass the counter.
in awk
, $0
corresponds to the record (here the line), and $i
for the fields i=1,2,3...
even number is the one with remainder 0 divided by 2. NR
is the line number.
Upvotes: 1
Reputation: 22247
Please update your question to be meaningful: There is no relationship between $0
and the Unix operating system, as choroba already pointed out in his comment. You obviously want to know the meaning of $0 in the awk programming language. From the awk man-page in the section about Fields:
$0 is the whole record, including leading and trailing whitespace.
Upvotes: 1