Using the awk command, I am to display lines 3-5 backwards of a file i have created and before the outputted line, the line number is to be displayed (i.e. line 3:). I am also to display the total wordcount of all three lines. My code is provided below. I keep obtaining an error message for the '%s' and not sure where to go from here, any help?
BEGIN { print("<< Start of file >>"); }
NR>=3 && NR<=5 { for (i = NF; i >= 1; i--)
printf "%d: %s ", $i;
print ""
wordCount += NF;
}
END { printf "<< End of file: wordCount = %d >>\n", wordCount }
Here's the input file:
Gimme presents I want more!
Gimme presents, I did my chores!
A bicycle, a tricycle, a motor vehicle!
I deserve it, you reverse it!
Gimme presents; more, more, more
Gimme presents I need more!
And the out put i obtain is:
(FILENAME=presents FNR=3) fatal: not enough arguments to satisfy format string
`%d: %s '
^ ran out for this one
%s
. Please post the exact text of error that you're getting.printf "%d: %s ", $i;
The%d
matches for$i
but there's nothing you provide to match for%s
. I'll put it in an answer shortly