Print the first line:
cat somefile.txt | awk ‘{print $1}’
… and see the number of active calls on Asterisk PBX:
asterisk -rx 'core show calls' | head -n 1 | awk '{print $1}'
Print all but first 6 columns of the file:
cat somefile.txt | awk ‘{$1=$2=$3=$4=$5=$6=””; print $0}’
… and see who’s trying to connect to 5060/TCP and 22/TCP (some fields reduced):
root@vds:~# tail -n 2 /var/log/syslog | awk '{$1=$2=$3=$4=$5=$6=""; print $0}' iptables denied Asterisk IN=eth0 SRC=69.60.119.204 DST=XX.XX.YY.ZZ PROTO=UDP SPT=5072 DPT=5060 iptables denied SSH IN=eth0 SRC=222.186.62.39 DST=XX.XX.YY.ZZ PROTO=TCP SPT=6000 DPT=22
By default, awk doesn’t separate columns. To do that, use comma:
root@vds:~# grep Aster /var/log/syslog | awk '{print $1,$2,$3,$9,$13}' Feb 17 07:03:25 Asterisk SRC=74.118.193.77 Feb 17 07:13:23 Asterisk SRC=85.25.194.185 Feb 17 07:56:31 Asterisk SRC=188.138.34.254 Feb 17 08:12:02 Asterisk SRC=200.12.49.147
PS: all these IPs are bad guys. They’re trying to connect to my SSH and SIP ports. So, I do not hide their addresses :)
Update:
In case you’ve got an output like
“123456”
“234567”
“345678”
and want to get rid of first and last symbols:
rev input_file.txt | cut -c2- | rev | cut -c2-
In case your columns have some other delimiters than space, p.e. comma, specify it:
cat file.txt | awk -F',' '{print $3}'
Tags: awk