Here are Some awk Command Examples for SSH, Possibly the Most Commonly Used Command Line Interface for Many of the Webmasters and Developers. Previously, we published this kind of brief guide on some of the frequently used GNU/Linux commands like echo, unix pipeline or this can be a better example – combining unix pipeline with grep, cut commands. As the title of this article says, our examples are limited within – awk command examples for SSH. We will give examples with web server software, like nginx.
Basic Theory About awk Command
AWK was created in ’70s and is the acronym of the authors “ Alfred Aho, Peter Weinberger, and Brian Kernighan. It is pronounced as auk
. When we are using all small caps – awk
– it means we are talking about the program that runs scripts written in the AWK programming language. We use GNU AWK re-written by Paul Rubin, Jay Fenlason, and Richard Stallman in 1988. AWK was preceded by sed. AWK is a data-driven scripting language, in very short.
So, technically we should write the title of the guides in this fashion – awk Command : Examples for SSH not AWK Command : Examples for SSH. We are not scripting in AWK language.
---
awk Command : Examples for SSH
Suppose, in our Nginx config file, this line is present :
1 2 3 4 5 | { ... access_log /var/log/nginx/thecustomizewindows.com.access.log; ... } |
First we will cd
to the location /var/log/nginx/
and run this command :
1 | awk '{print $3}' access.log | sort | uniq -c | sort -r |
Actually it works better it we have log_format
well defined. We will get the digits of HIT, BYPASS, EXPIRED and MISS like a table. If there is dash – that can mean HTTP 403. The near same result can be achieved by running a cat
command, this example of cat
is by Rahul Bansal (RTCamp) :
1 | cat access.log | cut -d '"' -f3 | cut -d ' ' -f2 | sort | uniq -c | sort -r |
What that {print $3}
part means? The command, awk ‘{foo}’ on foo file will apply the expression foo to each line of file and print the result to the terminal as standard output. awk splits its input lines on white space and saves each field as $1, $2 etc. awk reads each line of file foo and runs the script between {}. You can do a cat
of Nginx config file or that default
site config file :
1 | cat /etc/nginx/sites-available/default |
Run these to for understanding :
1 2 | awk '{print $3 }' /etc/nginx/sites-available/default awk '{print $1+0.45 " " $2 " " $3 }' /etc/nginx/sites-available/default |
Those $2
, $3
are frankly, “field”.
If we wanted to get same result like that of cat
against /etc/nginx/sites-available/default
, then we should run :
1 | awk '{print;}' /etc/nginx/sites-available/default |
There is no suck command like :
1 | awk /etc/nginx/sites-available/default |
You’ll get syntax error. We can print how many server lines are there by :
1 | awk 'END {print NR,"server"}' /etc/nginx/sites-available/default |
NR stands for number of records. It is a standard. $0
variable represents the entire current line, so print
and print $0
does exactly the same thing.
You can play with another example :
1 | awk -F":" '{ print $1 $3 }' /etc/passwd |
We using the -F
option to specify ":"
as the field separator. If we run :
1 | awk -F":" '{ print $1 " " $3 }' /etc/passwd |
then the individual results of $1
, " "
and $3
will get concatenated. root0
will look like root 0
. Obviously we can use awk
within the scripts, like this one for doing SSH on HP Cloud :
1 2 3 4 5 6 7 | ssh -t root@10.12.14.16 <<'EOF' awk ' NR==1 {max=0;min=1} NR>1 {if (max<$3) max=3} END {print max} ' FS="," /path/to/foo EOF |
change to your IP address, else you’ll get banned by that IP’s server’s security system. man awk
definitely going to help you.