User Tools

Site Tools


awk:awk_output

This is an old revision of the document!


AWK - AWK Output

Display Tidy Output

Multiple commands can be passed together. For instance issue the following command:

awk '/45/ {print $2,$3,$4} {print $1": "$5"\n"}' /sharewiz/awk/test.txt

which will display something like:

Peter Terence Roux
10: 45

Virginia Genevive Roux
11: 45

12: 5

13: 48

14: 16

48: 23
  • This inserts a colon and a space and comma between $1 and $5.
  • It also inserts new line after each two-line display.

Certain escape sequences are supported, including:

CharacterDetails
\\A literal backslash.
\bBackspace.
\fFormfeed.
\nNew line.
\rCarriage return.
\tHorizontal tab.
\vVertical tab.
\xhhHex digits where hh represents the hex digits. E.g. \x2F

Adding Text

Additional text can be added to lines. For instance issue the following command:

awk '/45/ {print "NAME: "$2,$3,$4 "\tAGE: "$5}' /sharewiz/awk/test.txt

which will display something like:

NAME: Peter Terence Roux	AGE: 45
NAME: Virginia Genevive Roux	AGE: 45

The values between double quotes can be anything.


Advanced Text Output

For more complex manipulation of text issue the following command:

awk '/45/ {printf ("%s %d %x %s\n", $2,$5,$5,"x")}' /sharewiz/awk/test.txt

which will display something like:

Peter 45 2d x
Virginia 45 2d x

The printf command can be used to handle more complex output requirements.

This is the list of specifications supported by the printf command:

SpecificationDetails
%cPrints a single ASCII character.
%dPrints a decimal number.
%ePrints a scientific notation representation of numbers.
%fPrints a floating-point representation.
%gPrints %e or %f; whichever is shorter.
%oPrints an unsigned octal number.
%sPrints an ASCII string.
%xPrints an unsigned hexadecimal number.
%%Prints a percent sign; no conversion is performed.

awk/awk_output.1588634974.txt.gz · Last modified: 2020/07/15 09:30 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki