Awk: Difference between revisions

From Psygen Wiki
Jump to navigation Jump to search
 
Line 50: Line 50:
<code> , </code> a comma represents a space.
<code> , </code> a comma represents a space.
Example: <code>awk '{print$2,$4}'</code> will print the 2nd column, a space, then the 4th column
Example: <code>awk '{print$2,$4}'</code> will print the 2nd column, a space, then the 4th column
<code> "String" </code> - prints the given string (remember to put the string inside double-quotes)


== References ==
== References ==

Latest revision as of 03:56, 16 May 2018

awk parses reports, among other text manipulation

Usage:

awk pattern {action}

Example:

awk '{print}' file.txt prints all of file.txt


awk '{print $3 "\t" $5}' file.txt prints just the third, and fifth columns of file.txt, separated by a tab.


Structure

BEGIN

BEGIN The begin block runs at the start of the awk program, and runs only once. The body block is optional.

BODY

/pattern/ {awk-commands} The body block runs on every line of text (this can be restricted with patterns.) There is no keyword for this block.

END

END {awk-commands} The END command runs once at the end of the program.


Variables

' (single-quote) Specify awk commands inside single quotes at the command line.
Example: awk '{print}' file.txt prints the contents of file.txt to the screen


$ field (column) reference
Example: $2 tells awk to do something to the 2nd field (column) of the input. (Note that awk does not interpret variables inside strings.)


$NF The last field or column. Useful in, say, domlogs, where the IP might be the last column, but might be in the 5th column on some lines, or the 6th on others. You can use $NF to still pull it.


\ The \ tells awk that a special character follows the slash, and to interpret it differently than regular text
Example:
\t means a tab character
\n a new line


, a comma represents a space. Example: awk '{print$2,$4}' will print the 2nd column, a space, then the 4th column


"String" - prints the given string (remember to put the string inside double-quotes)

References

  1. awk man page
  2. awk tutorial