{"id":5233,"date":"2015-10-04T13:27:39","date_gmt":"2015-10-04T05:27:39","guid":{"rendered":"http:\/\/rmohan.com\/?p=5233"},"modified":"2015-10-04T13:27:39","modified_gmt":"2015-10-04T05:27:39","slug":"use-awk-command-in-linux","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=5233","title":{"rendered":"use awk command in linux?"},"content":{"rendered":"<p>1. list content of file myfile<br \/>\n# [localhost@localhost ~] $ cat myfile<br \/>\napple<br \/>\norange<br \/>\nmango<\/p>\n<p>banana<br \/>\nGUAVA<br \/>\nra dish<br \/>\npineapple<\/p>\n<p>2. delete the first line of the file<br \/>\n# [localhost@localhost ~] $ sed &#8216;1d&#8217; myfile<br \/>\norange<br \/>\nmango<\/p>\n<p>banana<br \/>\nGUAVA<br \/>\nra dish<br \/>\npineapple<\/p>\n<p>3. delete the third line of the file<br \/>\n# [localhost@localhost ~] $ sed &#8216;3d&#8217; myfile<br \/>\napple<br \/>\norange<\/p>\n<p>banana<br \/>\nGUAVA<br \/>\nra dish<br \/>\npineapple<\/p>\n<p>4. delete the last line of the file<br \/>\n# [localhost@localhost ~] $ sed &#8216;$d&#8217; myfile<br \/>\napple<br \/>\norange<br \/>\nmango<\/p>\n<p>banana<br \/>\nGUAVA<br \/>\nra dish<\/p>\n<p>5. delete the 2nd and 4th line of the file<br \/>\n# [localhost@localhost ~] $ sed &#8216;2,4d&#8217; myfile<br \/>\napple<br \/>\nbanana<br \/>\nGUAVA<br \/>\nra dish<br \/>\npineapple<\/p>\n<p>6. delete except the 2nd and 4th line of the file<br \/>\n# [localhost@localhost ~] $ sed &#8216;2,4!d&#8217; myfile<br \/>\norange<br \/>\nmango<\/p>\n<p>7. delete the 1st and last line of the file<br \/>\n# [localhost@localhost ~] $ sed &#8216;1d;$d&#8217; myfile<br \/>\norange<br \/>\nmango<\/p>\n<p>banana<br \/>\nGUAVA<br \/>\nra dish<\/p>\n<p>8. delete all lines beginning with character &#8216;a&#8217;<br \/>\n# [localhost@localhost ~] $ sed &#8216;\/^a\/d&#8217; myfile<br \/>\norange<br \/>\nmango<\/p>\n<p>banana<br \/>\nGUAVA<br \/>\nra dish<br \/>\npineapple<\/p>\n<p>9. delete all lines ending with character &#8216;e&#8217;<br \/>\n# [localhost@localhost ~] $ sed &#8216;\/e$\/d&#8217; myfile<br \/>\nmango<\/p>\n<p>banana<br \/>\nGUAVA<br \/>\nra dish<\/p>\n<p>10. delete all lines ending with either &#8216;e&#8217; or &#8216;E&#8217;<br \/>\n# [localhost@localhost ~] $ sed &#8216;\/# [eE] $\/d&#8217; myfile<br \/>\nmango<\/p>\n<p>banana<br \/>\nGUAVA<br \/>\nra dish<\/p>\n<p>11. delete all the blank lines<br \/>\n# [localhost@localhost ~] $ sed &#8216;\/^$\/d&#8217; myfile<br \/>\napple<br \/>\norange<br \/>\nmango<br \/>\nbanana<br \/>\nGUAVA<br \/>\nra dish<br \/>\npineapple<\/p>\n<p>12. delete all lines which are entirely in UPPER CASE or CAPITAL LETTER<br \/>\n# [localhost@localhost ~] $ sed &#8216;\/^# [A-Z]*$\/d&#8217; myfile<br \/>\napple<br \/>\norange<br \/>\nmango<br \/>\nbanana<br \/>\nra dish<br \/>\npineapple<\/p>\n<p>13. delete all lines containing the pattern &#8216;an&#8217;<br \/>\n# [localhost@localhost ~] $ sed &#8216;\/an\/d&#8217; myfile<br \/>\napple<\/p>\n<p>GUAVA<br \/>\nra dish<br \/>\npineapple<\/p>\n<p>14. delete all lines not containing the pattern &#8216;an&#8217;<br \/>\n# [localhost@localhost ~] $ sed &#8216;\/an\/!d&#8217; myfile<br \/>\norange<br \/>\nmango<br \/>\nbanana<\/p>\n<p>15. delete all lines containing the pattern &#8216;an&#8217; or &#8216;le&#8217;<br \/>\n# [localhost@localhost ~] $ sed &#8216;\/an\\|le\/d&#8217; myfile<\/p>\n<p>GUAVA<br \/>\nra dish<\/p>\n<p>16. delete lines starting from 1st until meeting the PATTERN &#8216;banana&#8217;<br \/>\n# [localhost@localhost ~] $ sed &#8216;1,\/banana\/d&#8217; myfile<br \/>\nGUAVA<br \/>\nra dish<br \/>\npineapple<\/p>\n<p>17. delete lines meeting the PATTERN &#8216;banana&#8217; till the LAST line<br \/>\n# [localhost@localhost ~] $ sed &#8216;\/banana\/,$d&#8217; myfile<br \/>\napple<br \/>\norange<br \/>\nmango<\/p>\n<p>18. delete the last line only if it contains the PATTERN &#8216;apple&#8217;<br \/>\n# [localhost@localhost ~] $ sed &#8216;${\/apple\/d;}&#8217; myfile<br \/>\napple<br \/>\norange<br \/>\nmango<\/p>\n<p>banana<br \/>\nGUAVA<br \/>\nra dish<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. list content of file myfile # [localhost@localhost ~] $ cat myfile apple orange mango<\/p>\n<p>banana GUAVA ra dish pineapple<\/p>\n<p>2. delete the first line of the file # [localhost@localhost ~] $ sed &#8216;1d&#8217; myfile orange mango<\/p>\n<p>banana GUAVA ra dish pineapple<\/p>\n<p>3. delete the third line of the file # [localhost@localhost ~] $ sed [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,17],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5233"}],"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=5233"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5233\/revisions"}],"predecessor-version":[{"id":5234,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5233\/revisions\/5234"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}