{"id":833,"date":"2021-01-14T14:24:58","date_gmt":"2021-01-14T22:24:58","guid":{"rendered":"http:\/\/gantovnik.com\/bio-tips\/?p=833"},"modified":"2021-01-14T14:38:54","modified_gmt":"2021-01-14T22:38:54","slug":"147-count-the-total-number-of-fields-in-a-file-using-awk","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2021\/01\/147-count-the-total-number-of-fields-in-a-file-using-awk\/","title":{"rendered":"#147 Count the total number of fields in a file using awk"},"content":{"rendered":"<p>#147 Count the total number of fields in a file using awk<\/p>\n<p>Assume we have &#8220;test1.txt&#8221; file with data in columns:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\na b c d\r\ne f g h\r\ni j k l\r\n1 2 3 4\r\n<\/pre>\n<p>Save the following awk script in the file &#8220;ex147.awk&#8221;<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#!\/bin\/awk -f\r\n{ total += NF } \r\nEND { print total }\r\n<\/pre>\n<p>Run file &#8220;ex147run.txt&#8221;<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nawk -f ex147.awk test1.txt\r\n<\/pre>\n<p>using <\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n.\/ex147run.txt\r\n<\/pre>\n<p>The result is 16.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>#147 Count the total number of fields in a file using awk Assume we have &#8220;test1.txt&#8221; file with data in columns: a b c d e f g h i j k l 1 2 3 4 Save the following awk script in the file &#8220;ex147.awk&#8221; #!\/bin\/awk -f { total += NF } END { [&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_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":"","jetpack_post_was_ever_published":false},"categories":[18],"tags":[],"class_list":["post-833","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-dr","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":613,"url":"https:\/\/gantovnik.com\/bio-tips\/2020\/09\/93-summing-values-of-a-column-using-awk-command\/","url_meta":{"origin":833,"position":0},"title":"#93 Summing values of a column using awk","author":"gantovnik","date":"2020-09-09","format":false,"excerpt":"#93 Summing values of a column using awk command Assume we have \"test1.txt\" file with data in columns: [code language=\"python\"] a,a,aa,1 a,a,aa,2 d,d,dd,7 d,d,dd,9 d,dd,d,0 d,d,dd,23 d,d,dd,152 d,d,dd,7 d,d,dd,5 f2,f2,f2,5.5 [\/code] Save the following awk script in the file \"ex93.txt\" [code language=\"python\"] #The -F',' tells awk that the field separator\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":616,"url":"https:\/\/gantovnik.com\/bio-tips\/2020\/09\/94-average-of-values-of-a-column-using-awk\/","url_meta":{"origin":833,"position":1},"title":"#94 Average of  values of a column using awk","author":"gantovnik","date":"2020-09-09","format":false,"excerpt":"#94 Average of values of a column using awk command Assume we have \"test1.txt\" file with data in columns: [code language=\"python\"] a,a,aa,1 a,a,aa,2 d,d,dd,7 d,d,dd,9 d,dd,d,0 d,d,dd,23 d,d,dd,152 d,d,dd,7 d,d,dd,5 f2,f2,f2,5.5 [\/code] Save the following awk script in the file \"ex94.awk\" [code language=\"python\"] #!\/bin\/awk -f { sum += $4 }\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":842,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/01\/148-print-lines-containing-words-bar-gap-quad-and-count-number-of-lines-in-each-group-using-awk\/","url_meta":{"origin":833,"position":2},"title":"#148 Print lines containing words &#8220;BAR&#8221;, &#8220;GAP&#8221;, &#8220;QUAD&#8221; and count number of lines in each group using awk","author":"gantovnik","date":"2021-01-15","format":false,"excerpt":"#148 Print lines containing words \"BAR\", \"GAP\", \"QUAD\" and count number of lines in each group using awk Save the following awk script in the file \"ex148.awk\" [code language=\"python\"] #!\/bin\/awk -f \/GAP\/{print;n_gap++} \/BAR\/{print;n_bar++} \/QUAD\/{print;n_quad++} END { printf \"n_gap=%i\\n\",n_gap; printf \"n_bar=%i\\n\",n_bar;printf \"n_quad=%i\\n\",n_quad } [\/code] Run file \"ex148run.txt\" [code language=\"python\"] awk -f\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":833,"position":3},"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":1883,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/07\/357-joining-groups-of-lines-in-a-text-file-using-awk\/","url_meta":{"origin":833,"position":4},"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":1884,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/07\/358-remove-blank-lines-using-awk\/","url_meta":{"origin":833,"position":5},"title":"#358 Remove blank lines using awk","author":"gantovnik","date":"2023-07-15","format":false,"excerpt":"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 [code language=\"python\"] 1 2 3 4 5 6 7 8 9 [\/code] ex358.txt [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\/833","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=833"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/833\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}