{"id":622,"date":"2014-02-16T19:58:40","date_gmt":"2014-02-16T13:58:40","guid":{"rendered":"http:\/\/alexeyka.zantsev.com\/?p=622"},"modified":"2014-08-29T11:48:57","modified_gmt":"2014-08-29T05:48:57","slug":"awk","status":"publish","type":"post","link":"https:\/\/alexeyka.zantsev.com\/?p=622","title":{"rendered":"awk"},"content":{"rendered":"<p>Print the first line:<br \/>\ncat somefile.txt | <samp>awk &#8216;{print $1}&#8217;<\/samp><\/p>\n<p>&#8230; and see the number of active calls on Asterisk PBX:<\/p>\n<pre>\r\nasterisk -rx 'core show calls' | head -n 1 | awk '{print $1}'\r\n<\/pre>\n<p>Print all but first 6 columns of the file:<br \/>\ncat somefile.txt | <samp>awk &#8216;{$1=$2=$3=$4=$5=$6=&#8221;&#8221;; print $0}&#8217;<\/samp><\/p>\n<p>&#8230; and see who&#8217;s trying to connect to 5060\/TCP and 22\/TCP (some fields reduced):<\/p>\n<pre>\r\nroot@vds:~# tail -n 2 \/var\/log\/syslog | awk '{$1=$2=$3=$4=$5=$6=\"\"; print $0}'\r\n      iptables denied Asterisk IN=eth0 SRC=69.60.119.204 DST=XX.XX.YY.ZZ PROTO=UDP SPT=5072 DPT=5060\r\n      iptables denied SSH IN=eth0 SRC=222.186.62.39 DST=XX.XX.YY.ZZ PROTO=TCP SPT=6000 DPT=22\r\n<\/pre>\n<p>By default, awk doesn&#8217;t separate columns. To do that, use comma:<\/p>\n<pre>\r\nroot@vds:~# grep Aster \/var\/log\/syslog | awk '{print $1,$2,$3,$9,$13}'\r\nFeb 17 07:03:25 Asterisk SRC=74.118.193.77\r\nFeb 17 07:13:23 Asterisk SRC=85.25.194.185\r\nFeb 17 07:56:31 Asterisk SRC=188.138.34.254\r\nFeb 17 08:12:02 Asterisk SRC=200.12.49.147\r\n<\/pre>\n<p>PS: all these IPs are bad guys. They&#8217;re trying to connect to my SSH and SIP ports. So, I do not hide their addresses :)<\/p>\n<p>Update:<\/p>\n<p>In case you&#8217;ve got an output like<\/p>\n<p>&#8220;123456&#8221;<br \/>\n&#8220;234567&#8221;<br \/>\n&#8220;345678&#8221;<\/p>\n<p>and want to get rid of first and last symbols:<\/p>\n<pre>rev input_file.txt | cut -c2- | rev | cut -c2-<\/pre>\n<p>In  case your columns have some other delimiters than space, p.e. comma, specify it:<\/p>\n<pre>cat file.txt | awk -F',' '{print $3}'<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Print the first line: cat somefile.txt | awk &#8216;{print $1}&#8217; &#8230; and see the number of active calls on Asterisk PBX: asterisk -rx &#8216;core show calls&#8217; | head -n 1 | awk &#8216;{print $1}&#8217; Print all but first 6 columns of the file: cat somefile.txt | awk &#8216;{$1=$2=$3=$4=$5=$6=&#8221;&#8221;; print $0}&#8217; &#8230; and see who&#8217;s trying [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[163],"class_list":["post-622","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-awk"],"_links":{"self":[{"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=\/wp\/v2\/posts\/622","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=622"}],"version-history":[{"count":10,"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=\/wp\/v2\/posts\/622\/revisions"}],"predecessor-version":[{"id":738,"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=\/wp\/v2\/posts\/622\/revisions\/738"}],"wp:attachment":[{"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=622"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}