Gawk

From AdminWiki

(Difference between revisions)
Jump to: navigation, search
 
Line 1: Line 1:
== Problems and Solutions ==
== Problems and Solutions ==
 +
=== Get a certain column ===
 +
echo one two three | awk '{print $2}'
-
=== Get a columns from lines matching a pattern ===
+
=== Change the delimiter ===
 +
echo 'this;is;csv' | awk -F\; '{print $2}'
 +
Delimiter can be more than one character, but the whole string must be escaped:
 +
echo 'this@ SEP @is@ SEP @not@ SEP @csv' | awk -F@\ SEP\ @ '{print $2}'
 +
=== Get columns from lines matching a pattern ===
A small example explains it all:
A small example explains it all:
  netstat -s | awk '/active connections/ { print "active.value " $1 } /passive connection ope/ { print "passive.value " $1 } /failed connection/ { print "failed.value " $1 } /connection resets/ { print "resets.value " $1 } /connections established/ { print "established.value " $1 }'
  netstat -s | awk '/active connections/ { print "active.value " $1 } /passive connection ope/ { print "passive.value " $1 } /failed connection/ { print "failed.value " $1 } /connection resets/ { print "resets.value " $1 } /connections established/ { print "established.value " $1 }'

Latest revision as of 09:58, 10 April 2009

Contents

Problems and Solutions

Get a certain column

echo one two three | awk '{print $2}'

Change the delimiter

echo 'this;is;csv' | awk -F\; '{print $2}'

Delimiter can be more than one character, but the whole string must be escaped:

echo 'this@ SEP @is@ SEP @not@ SEP @csv' | awk -F@\ SEP\ @ '{print $2}'

Get columns from lines matching a pattern

A small example explains it all:

netstat -s | awk '/active connections/ { print "active.value " $1 } /passive connection ope/ { print "passive.value " $1 } /failed connection/ { print "failed.value " $1 } /connection resets/ { print "resets.value " $1 } /connections established/ { print "established.value " $1 }'
Personal tools