cola
August 18, 2009, 12:08pm
1
Hi,
Contents of BBS-list file:
foo
foo
foo
awk '
BEGIN { print "Analysis of \"foo\"" }
/foo/ { ++n }
END { print "\"foo\" appears", n, "times." }' BBS-list
Output:
Analysis of "foo"
"foo" appears 3 times.
awk '
BEGIN { print "Analysis of \"foo\"" }
/foo/ { ++n }
++n
END { print "\"foo\" appears", n, "times." }' BBS-list
Output:
Analysis of "foo"
foo
foo
foo
"foo" appears 6 times.
Why does it print the contents of BBS-list file if i add ++n?
cola:
Hi,
Contents of BBS-list file:
foo
foo
foo
awk '
BEGIN { print "Analysis of \"foo\"" }
/foo/ { ++n }
END { print "\"foo\" appears", n, "times." }' BBS-list
Output:
Analysis of "foo"
"foo" appears 3 times.
awk '
BEGIN { print "Analysis of \"foo\"" }
/foo/ { ++n }
++n
END { print "\"foo\" appears", n, "times." }' BBS-list
Output:
Analysis of "foo"
foo
foo
foo
"foo" appears 6 times.
Why does it print the contents of BBS-list file if i add ++n?
'++n' standing 'alone' is a condition with a missing 'action' - no corresponding '{...}'.
As '++n' (condition) is incremented and is NOT '0'. The default 'action' for a non-zero 'condition' is to print an entire current record/line.
Try this as example and see the output...
1 means true, in ur example, n++ is always positive so it prints for u all records...
awk '
BEGIN { print "Analysis of \"foo\"" }
/foo/ { ++n }1' BBS-list