In awk
there are two output functions: print
and printf
.
awk
very different?time
" on command line)?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. The format string is very similar to that in the ISO C library function printf() .
The AWK language is useful for manipulation of data files, text retrieval and processing. -F <value> - tells awk what field separator to use. In your case, -F: means that the separator is : (colon). '{print $4}' means print the fourth field (the fields being separated by : ).
awk '{ print $2; }' prints the second field of each line. This field happens to be the process ID from the ps aux output.
Default behavior of Awk: By default Awk prints every line of data from the specified file. In the above example, no pattern is given. So the actions are applicable to all the lines. Action print without any argument prints the whole line by default, so it prints all the lines of the file without failure.
print
function outputs a newline at the end; printf
does not unless requested.print
code converts the arguments to strings and then sends them to the output separated by the OFS (output field separator).printf
code might need to convert the string to a double before formatting it using a double format (%16.8g
or something), and similar operations.write(2)
or something similar for both, but there'll be code (probably <stdio.h>
) layered above that.All that adds up to:
print
is a little simpler (and therefore faster) than printf
.print
if it will do what you need; use printf
when it does what you need.And using a sprintf
followed by print
is likely to be slower than using printf
directly, so don't.
In case of doubt, measure.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With