{"id":172,"date":"2011-05-06T07:35:54","date_gmt":"2011-05-06T07:35:54","guid":{"rendered":"http:\/\/shivdev.com\/blog\/?p=172"},"modified":"2011-05-06T07:37:13","modified_gmt":"2011-05-06T07:37:13","slug":"delete-lines-in-a-log-file-using-shell-scripts","status":"publish","type":"post","link":"http:\/\/shivdev.com\/blog\/2011\/05\/06\/delete-lines-in-a-log-file-using-shell-scripts\/","title":{"rendered":"Delete Lines in a Log File using Shell Scripts"},"content":{"rendered":"<p>If you want to delete a PATTERN from a Log File.<\/p>\n<blockquote><p>\n$ sed &#8216;\/PATTERN\/d&#8217; server.log > newServer.log\n<\/p><\/blockquote>\n<p>If you want to write a small script to do it:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n#!\/bin\/sh\r\nfiles=&quot;\/home\/user\/app\/logs\/*.log&quot;\r\nfor f in $files\r\ndo\r\n  sed '\/PATTERN\/d' $f &gt; $f.out\r\n  mv  $f.out $f\r\ndone\r\n<\/pre>\n<p>Now if you want to do it for a bunch of log files, I wrote a script that works for me:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n#!\/bin\/sh\r\n\r\n# Check usage\r\nif [ $# -lt 2 ] ; then\r\n    echo &quot;Syntax: $0  &lt;DeleteString&gt; &lt;Directory\/Pattern&gt;&quot;\r\n    echo &quot;Example: $0 DefaultRequestDestination .\/logs\/*webser*.log*&quot;\r\n    exit 1\r\nfi\r\n\r\nPATTERN=$1\r\nshift\r\n\r\n# Print Warning Info: About to mess with your logs\r\nfor f in $@\r\ndo\r\n\techo &quot;Will Delete Lines Containing: $PATTERN in file: $f&quot;\r\ndone\r\n\r\nread -p &quot;Are you sure? &quot; -n 1\r\nif [[ $REPLY =~ ^[Yy]$ ]]; then\r\n\r\n\techo &quot;OK Cleaning... &quot;\r\n\r\n\t# Loop over files pattern\r\n\tfiles=$@ \r\n\tfor f in $files\r\n\tdo\r\n\t\techo &quot;Cleaning: $f&quot;\r\n\t\tsed &quot;\/&quot;${PATTERN}&quot;\/d&quot; $f &gt; $f.out\r\n\t\tmv  $f.out $f\r\n\tdone\r\nfi\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you want to delete a PATTERN from a Log File. $ sed &#8216;\/PATTERN\/d&#8217; server.log > newServer.log If you want to write a small script to do it: Now if you want to do it for a bunch of log files, I wrote a script that works for me:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[13],"tags":[],"_links":{"self":[{"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/posts\/172"}],"collection":[{"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/comments?post=172"}],"version-history":[{"count":10,"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/posts\/172\/revisions"}],"predecessor-version":[{"id":196,"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/posts\/172\/revisions\/196"}],"wp:attachment":[{"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/media?parent=172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/categories?post=172"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/tags?post=172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}