{"id":3424,"date":"2014-08-06T01:08:10","date_gmt":"2014-08-05T17:08:10","guid":{"rendered":"http:\/\/rmohan.com\/?p=3424"},"modified":"2014-08-06T01:09:04","modified_gmt":"2014-08-05T17:09:04","slug":"grep-awk-and-sed-in-bash","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=3424","title":{"rendered":"Grep, Awk, and Sed in bash"},"content":{"rendered":"<p>So, let\u2019s consider the following log file:<\/p>\n<p>www.three.com 10.15.101.11 1353280801 TEST 345<br \/>\nwww.one.com 10.14.101.11 1353280801 TEST 343<br \/>\nwww.three.com 1.10.11.71 1353280801 TEST 323<br \/>\nwww.one.com 10.15.11.61 1353280801 TEST 365<br \/>\nwww.two.com 10.10.11.51 1353290801 TEST 55<br \/>\nwww.two.com 10.20.13.11 1353290801 REST 435<br \/>\nwww.one.com 10.20.14.41 1353290801 REST 65<br \/>\nwww.two.com 10.10.11.14 1353290801 REST 345<br \/>\nwww.three.com 10.10.11.31 1354280801 REST 34<br \/>\nwww.one.com 10.10.13.144 1354280801 JSON 65<br \/>\nwww.two.com 10.50.11.141 1354280801 JSON 665<br \/>\nwww.three.com 120.10.11.11 1354280801 JSON 555<br \/>\nwww.two.com 10.144.11.11 1383280801 RAW 33<br \/>\nwww.one.com 10.103.141.141 1383280801 RAW 315<\/p>\n<p>Now, here are some things you can do to this log file:<br \/>\nHow many files are in a directory: ls | wc -l<br \/>\nPrint the file: cat sample.log<br \/>\nPrint lines that match a particular word: grep \u201cRAW\u201d sample.log<br \/>\nPrint those lines to a file called test.log: grep \u201cRAW\u201d sample.log > test.log<br \/>\nPrint particular columns and sort: cat sample.log | awk \u2018{ print $1,$2}\u2019 | sort -k 1<br \/>\nFind and Replace using SED and Regex: cat sample.log | sed \u2018s\/TEST\/JSON\/g\u2019<br \/>\nSplit a log file into multiple files using a column as name with AWK: awk \u2018{ print >>($4?.log\u201d); close($4?.log\u201d) }\u2019 sample.log<br \/>\nUse substr (removes last character) in AWK to manipulate a string per line: cat sample.log | awk \u2018{ print $1,$2,$3,substr($4,1,length($4)-1),$5}\u2019<br \/>\nPrint first line of file with SED: sed q test.log<br \/>\nPrint last line of file with SED: sed \u2018$!d\u2019 sample.log<br \/>\nPerform a regular expression on last character of entire file using SED: cat sample.log | sed \u2018$ s\/5$\/\/\u2019<br \/>\nAdd some text to beginning and end of a file with AWK: cat sample.log | awk \u2018BEGIN{print \u201cSTART\u201d } { print } END{print \u201cEND\u201d}\u2019<br \/>\nCount and print how many unique fields are in all rows using AWK: cat sample.log | awk \u2018{ if (a[$1]++ == 0) print $1 }\u2019 | wc -l<br \/>\nMake everything lowercase with AWK: cat sample.log | awk \u2018{print tolower($0)}\u2019<br \/>\nMultiple SED regular expressions: sed \u20191s\/^\/START\/;$ s\/5$\/END\/\u2019 sample.log<br \/>\nRegex with SED on multiple files: for file in *; do sed \u20191s\/^\/START\/\u2019 $file > $file\u2019.json\u2019; done<\/p>\n<p>n a command on each file in directory: for i in `ls`; do $i; done<br \/>\nRename the extension of all files in a folder: for old in *.txt; do mv $old `basename $old .txt`.json; done<br \/>\nMerge all files in a directory with a comma seperator: find . -type f -not -name output.txt -exec cat {} ; -exec echo `,` ; > output.txt<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So, let\u2019s consider the following log file:<\/p>\n<p>www.three.com 10.15.101.11 1353280801 TEST 345 www.one.com 10.14.101.11 1353280801 TEST 343 www.three.com 1.10.11.71 1353280801 TEST 323 www.one.com 10.15.11.61 1353280801 TEST 365 www.two.com 10.10.11.51 1353290801 TEST 55 www.two.com 10.20.13.11 1353290801 REST 435 www.one.com 10.20.14.41 1353290801 REST 65 www.two.com 10.10.11.14 1353290801 REST 345 www.three.com 10.10.11.31 1354280801 REST 34 www.one.com 10.10.13.144 1354280801 [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3424"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3424"}],"version-history":[{"count":2,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3424\/revisions"}],"predecessor-version":[{"id":3426,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3424\/revisions\/3426"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3424"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3424"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}