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

11
  • 1
    Was it really too hard to read the manual for something like this? Commented Nov 6, 2015 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 =) Commented Nov 6, 2015 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. Commented Nov 6, 2015 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? Commented Nov 6, 2015 at 6:03
  • @RobertL see the mouse over text for a downvote: "This question does not show any research effort..." Commented Nov 6, 2015 at 7:02

2 Answers 2

8

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.

1
  • 3
    good answer but examples of print and printf would be better Commented Jul 31, 2016 at 22:13
5

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.