{"id":1884,"date":"2023-07-15T02:26:09","date_gmt":"2023-07-15T09:26:09","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=1884"},"modified":"2023-07-15T03:50:40","modified_gmt":"2023-07-15T10:50:40","slug":"358-remove-blank-lines-using-awk","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2023\/07\/358-remove-blank-lines-using-awk\/","title":{"rendered":"#358 Remove blank lines using awk"},"content":{"rendered":"<p>Remove blank lines using awk.<\/p>\n<p>NF variable in awk holds the number of fields in the line. In case of empty line, NF is zero and the condition is not met, so awk does nothing.<\/p>\n<p>example.txt<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n1\r\n\r\n2\r\n\r\n3\r\n\r\n4\r\n\r\n5\r\n\r\n6\r\n\r\n7\r\n\r\n8\r\n\r\n9\r\n<\/pre>\n<p>ex358.txt<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nawk 'NF' example.txt\r\n<\/pre>\n<p>output:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Remove blank lines using awk. NF variable in awk holds the number of fields in the line. In case of empty line, NF is zero and the condition is not met, so awk does nothing. example.txt 1 2 3 4 5 6 7 8 9 ex358.txt awk &#8216;NF&#8217; example.txt output: 1 2 3 4 5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","_lmt_disableupdate":"yes","_lmt_disable":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[18],"tags":[],"class_list":["post-1884","post","type-post","status-publish","format-standard","hentry","category-awk"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-uo","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1889,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/07\/361-print-the-number-of-fields-in-each-line-containing-more-than-10-fields-is-the-field-delimiter\/","url_meta":{"origin":1884,"position":0},"title":"#361 Print the number of fields in each line containing more than 10 fields. &#8220;,&#8221; is the field delimiter.","author":"gantovnik","date":"2023-07-15","format":false,"excerpt":"Print the number of fields in each line containing more than 10 fields. \",\" is the field delimiter. example.txt [code language=\"python\"] GAP,5000008,344229,-0.00000873,0.00000000,0.00000000 QUAD,5000009,389633,-62.98719000,4.20363900,-14.08507000,-5.83411800,-2.50325100,-4.89406400,14.13978000,1.43138700 TRIA,5000009,389634,-120.91420000,150.57080000,-76.72402000,0.96178280,-0.36827340,1.96346300,-1.02116100,-41.11020000 QUAD,5000009,389635,-15.21044000,0.99932950,3.18402900,-0.15678900,0.23568760,-0.34962440,0.21704630,-0.38061680 QUAD,5000009,389636,1.33232100,-65.55149000,11.67951000,-0.72253810,-0.16559730,0.68029730,-0.87976630,0.07846473 BUSH,5000008,377944,107.45720000,25.92157000,3.61063300,2.17414500,0.00000000,0.00000000 BUSH,5000008,377945,108.76870000,25.35708000,3.30875900,2.66002100,0.00000000,0.00000000 [\/code] ex361.txt [code language=\"python\"] awk -F\",\" '{if(NF > 10) print NF,\":\",$0}' example.txt [\/code] output: [code language=\"python\"] 11 : QUAD,5000009,389633,-62.98719000,4.20363900,-14.08507000,-5.83411800,-2.50325100,-4.89406400,14.13978000,1.43138700 11 : TRIA,5000009,389634,-120.91420000,150.57080000,-76.72402000,0.96178280,-0.36827340,1.96346300,-1.02116100,-41.11020000\u2026","rel":"","context":"In &quot;awk&quot;","block_context":{"text":"awk","link":"https:\/\/gantovnik.com\/bio-tips\/category\/awk\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1886,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/07\/360-print-the-number-of-fields-in-each-line-where-using-awk-is-the-field-delimiter\/","url_meta":{"origin":1884,"position":1},"title":"#360 Print the number of fields in each line where using awk. &#8220;,&#8221; is the field delimiter.","author":"gantovnik","date":"2023-07-15","format":false,"excerpt":"Print the number of fields in each line using awk. \",\" is the field delimiter. example.txt [code language=\"python\"] GAP,5000008,344229,-0.00000873,0.00000000,0.00000000 QUAD,5000009,389633,-62.98719000,4.20363900,-14.08507000,-5.83411800,-2.50325100,-4.89406400,14.13978000,1.43138700 TRIA,5000009,389634,-120.91420000,150.57080000,-76.72402000,0.96178280,-0.36827340,1.96346300,-1.02116100,-41.11020000 QUAD,5000009,389635,-15.21044000,0.99932950,3.18402900,-0.15678900,0.23568760,-0.34962440,0.21704630,-0.38061680 QUAD,5000009,389636,1.33232100,-65.55149000,11.67951000,-0.72253810,-0.16559730,0.68029730,-0.87976630,0.07846473 BUSH,5000008,377944,107.45720000,25.92157000,3.61063300,2.17414500,0.00000000,0.00000000 BUSH,5000008,377945,108.76870000,25.35708000,3.30875900,2.66002100,0.00000000,0.00000000 [\/code] ex360.txt [code language=\"python\"] awk -F\",\" '{print NF,\":\",$0}' example.txt [\/code] output: [code language=\"python\"] 6 : GAP,5000008,344229,-0.00000873,0.00000000,0.00000000 11 : QUAD,5000009,389633,-62.98719000,4.20363900,-14.08507000,-5.83411800,-2.50325100,-4.89406400,14.13978000,1.43138700 11 : TRIA,5000009,389634,-120.91420000,150.57080000,-76.72402000,0.96178280,-0.36827340,1.96346300,-1.02116100,-41.11020000 11 : QUAD,5000009,389635,-15.21044000,0.99932950,3.18402900,-0.15678900,0.23568760,-0.34962440,0.21704630,-0.38061680\u2026","rel":"","context":"In &quot;awk&quot;","block_context":{"text":"awk","link":"https:\/\/gantovnik.com\/bio-tips\/category\/awk\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1885,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/07\/359-print-the-number-of-fields-in-each-line-using-awk\/","url_meta":{"origin":1884,"position":2},"title":"#359 Print the number of fields in each line using awk","author":"gantovnik","date":"2023-07-15","format":false,"excerpt":"Print the number of fields in each line using awk. example.txt [code language=\"python\"] QUAD 5000009 389633 -62.98719000 4.20363900 -14.08507000 -5.83411800 -2.50325100 -4.89406400 14.13978000 1.43138700 TRIA 5000009 389634 -120.91420000 150.57080000 -76.72402000 0.96178280 -0.36827340 1.96346300 -1.02116100 -41.11020000 QUAD 5000009 389635 -15.21044000 0.99932950 3.18402900 -0.15678900 0.23568760 -0.34962440 0.21704630 -0.38061680 GAP 5000008 344229 -0.00000873\u2026","rel":"","context":"In &quot;awk&quot;","block_context":{"text":"awk","link":"https:\/\/gantovnik.com\/bio-tips\/category\/awk\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1883,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/07\/357-joining-groups-of-lines-in-a-text-file-using-awk\/","url_meta":{"origin":1884,"position":3},"title":"#357 Joining groups of lines in a text file using awk","author":"gantovnik","date":"2023-07-14","format":false,"excerpt":"Joining groups of lines in a text file. The prints three consecutive lines of text on the same line, after which a linefeed is printed. This has the effect of \"joining\" every three consecutive lines of text. example.txt [code language=\"python\"] 1 2 3 4 5 6 7 8 9 [\/code]\u2026","rel":"","context":"In &quot;awk&quot;","block_context":{"text":"awk","link":"https:\/\/gantovnik.com\/bio-tips\/category\/awk\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":867,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/02\/156-filtering-content-from-files-using-awk\/","url_meta":{"origin":1884,"position":4},"title":"#156 Filtering content from files using awk","author":"gantovnik","date":"2021-02-09","format":false,"excerpt":"#156 Displaying and filtering the content of files with awk Using the following command, we can print all lines from the file: [code language=\"python\"] awk ' { print } ' \/etc\/passwd [\/code] This is equivalent to using the $0 variable. The $0 variables refers to the complete line. [code language=\"python\"]\u2026","rel":"","context":"In &quot;awk&quot;","block_context":{"text":"awk","link":"https:\/\/gantovnik.com\/bio-tips\/category\/awk\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":833,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/01\/147-count-the-total-number-of-fields-in-a-file-using-awk\/","url_meta":{"origin":1884,"position":5},"title":"#147 Count the total number of fields in a file using awk","author":"gantovnik","date":"2021-01-14","format":false,"excerpt":"#147 Count the total number of fields in a file using awk Assume we have \"test1.txt\" file with data in columns: [code language=\"python\"] a b c d e f g h i j k l 1 2 3 4 [\/code] Save the following awk script in the file \"ex147.awk\" [code\u2026","rel":"","context":"In &quot;awk&quot;","block_context":{"text":"awk","link":"https:\/\/gantovnik.com\/bio-tips\/category\/awk\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1884","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/comments?post=1884"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1884\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=1884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=1884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=1884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}