Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the following script in awk that sums all columns in a file that I pipe to it:

{   for (i=1; i<=NF; i++) { sum[i]+= $i }   }

END { for (i=1; i<=NF; i++ ) { printf "%d ", sum[i] } }

e.g.: cat my_file.txt | awk -f sum_all.awk

with my_file.txt:

5 7 8
1 0 2


6 7 10 %

How can I get rid of that last character %?

share|improve this question
What is your shell prompt? I bet it gets evaluated to “%”. – manatwork Dec 12 '12 at 16:22
@manatwork It is zsh. Why? – Amelio Vazquez-Reina Dec 12 '12 at 16:27
Because your assumption of “Extra % in output of awk script” is wrong. The “%” is not part of awk's output, most probably is the prompt displayed after the command finished. – manatwork Dec 12 '12 at 16:32
@manatwork for future reference, zsh displays a (reversed) % when the output doesn't end with a newline. Compare bash vs zsh. – Kevin Dec 12 '12 at 18:20
Thank you, @Kevin. – manatwork Dec 13 '12 at 7:55
up vote 1 down vote accepted

As manatwork already commented, the % is not part of the output from awk, it's the next prompt from the shell. In the END block, for this input file, there are three calls to printf. The first outputs 6 and a space, the second outputs 7 and a space, and the third outputs 10 and a space. After this, awk exits, and the shell prints its prompt.

If a command prints some output that does not end in a newline (or, more generally, if it doesn't leave the cursor at the beginning of a line), then depending on your shell's configuration, the shell will either print its prompt after the command's output on the same line, or the shell might erase the unterminated line and print its prompt at the beginning of the line.

To make sure a command's output is fully visible, make sure that it ends in a newline (unless the command produces no output, of course). In unix systems, a non-empty text file always ends with a newline, because a text files consists of a (possibly empty) series of lines, each of which consists of a (possibly empty) series of characters other than newline (and null bytes). Most utilities tend to be designed to deal primarily with text files, so make sure that your command's output is a valid text file.

After printing the fields, print a "\n" (the awk notation for a newline character), or call the print function, which adds a newline after the printed text.

END { for (i=1; i<=NF; i++) printf "%d ", sum[i]; print ""; }

or, to avoid having an extra space at the end of the line:

END { for (i=1; i<NF; i++) printf "%d ", sum[i]; printf "%d\n", sum[NF]; }


END { printf "%d"; for (i=2; i<=NF; i++) printf " %d", sum[i]; print ""; }


END { for (i=1; i<NF; i++ ) printf "%d%s", sum[i], (i==NR ? "\n" : " "); }
share|improve this answer
END { for (i=1; i<=NF; i++ ) { printf "%d ", sum[i] } ; print "" }
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.