RULE OF THUMB FOR AWK: awk runs once per line (unless BEGIN or END script in which it runs once in start or end of script, or if you use /grep-search-expression/ then it runs once per search success)

 

awk -F “delimtier” ‘{ print $1 }’ inputfile > outputfile

 

or with setting the FS variable (Field Seperator in the awk program instead of in an argument):

 

awk ‘BEGIN { FS=”:” } {print $1}’ inputfile > outputfile

cut looks like this – cut is made for this

 

cut -f1 -d: inputfile > outputfile

 

or with quotes on delimiter

 

cut -f1 -d”:” inputfile > outputfile

 

The upside is that AWK can use string delimiters where as CUT  needs character delimiters

 

THINGS TO NOTE:

 

In cut the -d is the delimiter and the -f is the field number both are lower case.

 

In awk there is no -d just a -F which is the delimiter… (im sure awk has a -d Im just not talking about it right now)

 

SO WHAT DOES THIS DO?

 

This picks everything to the left of the first :, colon, it encounters on every line

 

awk -F”:” ‘{print $1}’ inputfile > outputfile

 

This is the same as:

 

awk ‘BEGIN { FS=”:” } {print $1}’

 

Here is a way to do these same lines without inputfile, outputfile but instead with pipes:

 

netstat -ntulp | awk ‘{ print $4}’ | cut -f1 -d”:”

netstat -ntulp | awk ‘{ print $4}’ | awk -F”:” ‘{print $1}’

netstat -ntulp | awk ‘{ print $4}’ | awk ‘BEGIN { FS=”:” } {print $1}’

 

OUTPUT EXAMPLE:

root@debikos71:/var/www# netstat -ntulp | awk ‘{ print $4}’

(only

Local

0.0.0.0:111

0.0.0.0:46485

0.0.0.0:22

127.0.0.1:25

:::111

:::43120

:::22

::1:25

:::50500

0.0.0.0:111

0.0.0.0:702

127.0.0.1:734

0.0.0.0:5353

0.0.0.0:50946

0.0.0.0:1900

0.0.0.0:44420

:::49219

:::111

:::702

:::5353

:::57121

 

ALL OF THE TOP COMMANDS OUTPUT ( I removed the extra spaces etc):

(only

Local

0.0.0.0

0.0.0.0

0.0.0.0

127.0.0.1

0.0.0.0

0.0.0.0

127.0.0.1

0.0.0.0

0.0.0.0

0.0.0.0

0.0.0.0

Leave a Reply

Your email address will not be published. Required fields are marked *