The Print Command
The print command can be followed by variables, fields, or text in quotes. If commas separate the items, a space will be inserted between the items in the output line.
- {print “user” $1} would merge the string “user” with the contents of field 1
- {print “user”, $1} would place a comma between the string “user” and the contents of field 1
- Examples: awk_prog5 and awk_prog5a using awk_data5
- Examples: awk_prog5b using awk_data5b. (This is a bash script.)
The Printf Command
The printf command is a formatted print.
- Syntax: printf (“string” , field, ... )
- Fields are inserted into the “string” where percent signs (%) occur.
- Percent signs (%) are followed by a letter to designate the type of field.
- Types of fields include:
- d – decimal integer
- f – floating point
- s – string
- x – hexadecimal
- Unlike the print command, there is no automatic EOL character added.You must use “\n”.
- Numbers can be placed between the “%” and the letter to control the size of a field.
- A decimal point(.) followed by a number can be used to control the number of digits after the decimal point.
- A minus(-) sign can be used after the “%” to left justify the field.
- See examples awk_prog6a-e which use awk_data6.