7

In awk what is the difference between print and printf?

Is there any restrictions to use only print instead of printf and vice-versa

  • 1
    Was it really too hard to read the manual for something like this? – jasonwryan Nov 6 '15 at 3:20
  • 1
    @jasonwryan Agree- But if I repeatedly ask about something which is available at manual, your comment is more useful. But it was my first post. I t was very odd behavior from technically sound person =) – Ravan Nov 6 '15 at 4:23
  • Ravan, on U&L context is appreciated. Such as: I am learning, saw this, tried that, this happened, I want to do this, why, and how? Working examples to illustrate are great! There are hundreds of perspectives on a question like yours -- a little context to get a direction and boot up an answer really helps. Whatever makes you to wonder would be good to know. I see you're a top member on AskUbuntu, welcome to U&L. I would say one way to think about the difference is that Ubuntu is a product, Unix is philosophy. – RobertL Nov 6 '15 at 5:55
  • @jasonwryan I also think the question is too general. But I see Ravan's point. How about: "Most of what you're asking is covered in the awk manpage." Can we judge what might be hard or easy for someone else? – RobertL Nov 6 '15 at 6:03
  • @RobertL see the mouse over text for a downvote: "This question does not show any research effort..." – jasonwryan Nov 6 '15 at 7:02
7

As the GNU awk (gawk manual says) at http://www.gnu.org/software/gawk/manual/gawk.html#Printing

Use the print statement to produce output with simple, standardized formatting. You specify only the strings or numbers to print, in a list separated by commas. They are output, separated by single spaces, followed by a newline.

& etc.

For more precise control over the output format than what is provided by print, use printf. With printf you can specify the width to use for each item, as well as various formatting choices for numbers (such as what output base to use, whether to print an exponent, whether to print a sign, and how many digits to print after the decimal point).

So this pretty much explains the differences and restrictions.

  • 2
    good answer but examples of print and printf would be better – HattrickNZ Jul 31 '16 at 22:13
4

print:

  • "Print" by itself prints the input line.
  • "Print" with one argument prints the argument.
  • "Print" with multiple arguments prints all the arguments, separated by spaces (or other specified OFS) when the arguments are separated by commas, or concatenated when the arguments are separated by spaces.

printf:

The "printf()" (formatted print) function is much more flexible, and trickier. It has the syntax:

 printf(<string>,<expression list>)

Difference:

The difference between printf and print is the format argument. This is an expression whose value is taken as a string; it specifies how to output each of the other arguments. It is called the format string.

References here and here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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