{"id":1886,"date":"2023-07-15T03:48:33","date_gmt":"2023-07-15T10:48:33","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=1886"},"modified":"2023-07-15T03:48:33","modified_gmt":"2023-07-15T10:48:33","slug":"360-print-the-number-of-fields-in-each-line-where-using-awk-is-the-field-delimiter","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2023\/07\/360-print-the-number-of-fields-in-each-line-where-using-awk-is-the-field-delimiter\/","title":{"rendered":"#360 Print the number of fields in each line where using awk. &#8220;,&#8221; is the field delimiter."},"content":{"rendered":"<p>Print the number of fields in each line using awk.<br \/>\n&#8220;,&#8221; is the field delimiter.<\/p>\n<p>example.txt<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nGAP,5000008,344229,-0.00000873,0.00000000,0.00000000\r\nQUAD,5000009,389633,-62.98719000,4.20363900,-14.08507000,-5.83411800,-2.50325100,-4.89406400,14.13978000,1.43138700\r\nTRIA,5000009,389634,-120.91420000,150.57080000,-76.72402000,0.96178280,-0.36827340,1.96346300,-1.02116100,-41.11020000\r\nQUAD,5000009,389635,-15.21044000,0.99932950,3.18402900,-0.15678900,0.23568760,-0.34962440,0.21704630,-0.38061680\r\nQUAD,5000009,389636,1.33232100,-65.55149000,11.67951000,-0.72253810,-0.16559730,0.68029730,-0.87976630,0.07846473\r\nBUSH,5000008,377944,107.45720000,25.92157000,3.61063300,2.17414500,0.00000000,0.00000000\r\nBUSH,5000008,377945,108.76870000,25.35708000,3.30875900,2.66002100,0.00000000,0.00000000\r\n<\/pre>\n<p>ex360.txt<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nawk -F&quot;,&quot; '{print NF,&quot;:&quot;,$0}' example.txt\r\n<\/pre>\n<p>output:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n6 : GAP,5000008,344229,-0.00000873,0.00000000,0.00000000\r\n11 : QUAD,5000009,389633,-62.98719000,4.20363900,-14.08507000,-5.83411800,-2.50325100,-4.89406400,14.13978000,1.43138700\r\n11 : TRIA,5000009,389634,-120.91420000,150.57080000,-76.72402000,0.96178280,-0.36827340,1.96346300,-1.02116100,-41.11020000\r\n11 : QUAD,5000009,389635,-15.21044000,0.99932950,3.18402900,-0.15678900,0.23568760,-0.34962440,0.21704630,-0.38061680\r\n11 : QUAD,5000009,389636,1.33232100,-65.55149000,11.67951000,-0.72253810,-0.16559730,0.68029730,-0.87976630,0.07846473\r\n9 : BUSH,5000008,377944,107.45720000,25.92157000,3.61063300,2.17414500,0.00000000,0.00000000\r\n9 : BUSH,5000008,377945,108.76870000,25.35708000,3.30875900,2.66002100,0.00000000,0.00000000\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Print the number of fields in each line using awk. &#8220;,&#8221; is the field delimiter. example.txt 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 ex360.txt awk -F&quot;,&quot; &#8216;{print NF,&quot;:&quot;,$0}&#8217; example.txt output: 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 11 : QUAD,5000009,389636,1.33232100,-65.55149000,11.67951000,-0.72253810,-0.16559730,0.68029730,-0.87976630,0.07846473 9 : BUSH,5000008,377944,107.45720000,25.92157000,3.61063300,2.17414500,0.00000000,0.00000000 9 : BUSH,5000008,377945,108.76870000,25.35708000,3.30875900,2.66002100,0.00000000,0.00000000<\/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-1886","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-uq","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":1886,"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":1885,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/07\/359-print-the-number-of-fields-in-each-line-using-awk\/","url_meta":{"origin":1886,"position":1},"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":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":1886,"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":1899,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/07\/366-print-lines-in-a-range-using-awk\/","url_meta":{"origin":1886,"position":3},"title":"#366 Print lines in a range using awk","author":"gantovnik","date":"2023-07-20","format":false,"excerpt":"example.csv [code language=\"python\"] Type,LCID,EID,Nx,Ny,Nxy,Mx,My,Mxy,Qyz,Qxz QUAD,5000001,389635,5.21044,0.9993295,3.184029,0.228490059,0.762438914,0.349926963,0.007402616,0.645762323 QUAD,5000002,389636,4.332321,-45.55149,10.13951,0.128933201,0.289045003,0.442976734,0.394346902,0.965424659 QUAD,5000003,389637,5.332321,-65.55149,12.32391,0.867613103,0.345799842,0.902593358,0.56308979,0.77084519 QUAD,5000004,389638,4.332321,-35.55149,14.27251,0.429855813,0.882431001,0.15453015,0.710809946,0.918974895 QUAD,5000005,389639,5.332455,-75.55149,15.37321,0.738401857,0.289007109,0.59902178,0.824840285,0.571976301 QUAD,5000006,389640,4.332321,-65.55149,13.27221,0.102678011,0.581496802,0.785601755,0.415283869,0.817283531 QUAD,5000007,389641,3.332321,-45.55149,12.67351,0.363658748,0.803570041,0.70688413,0.459446702,0.330453157 [\/code] ex366.txt [code language=\"python\"] awk 'NR>2 && NR < 5' example.csv [\/code] output: [code language=\"python\"] QUAD,5000002,389636,4.332321,-45.55149,10.13951,0.128933201,0.289045003,0.442976734,0.394346902,0.965424659 QUAD,5000003,389637,5.332321,-65.55149,12.32391,0.867613103,0.345799842,0.902593358,0.56308979,0.77084519 [\/code]","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":1901,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/07\/367-extracting-columns-of-the-data-file-using-awk\/","url_meta":{"origin":1886,"position":4},"title":"#367 Extracting columns of the data file using awk.","author":"gantovnik","date":"2023-07-20","format":false,"excerpt":"example.csv [code language=\"python\"] Type,LCID,EID,Nx,Ny,Nxy,Mx,My,Mxy,Qyz,Qxz QUAD,5000001,389635,5.21044,0.9993295,3.184029,0.228490059,0.762438914,0.349926963,0.007402616,0.645762323 QUAD,5000002,389636,4.332321,-45.55149,10.13951,0.128933201,0.289045003,0.442976734,0.394346902,0.965424659 QUAD,5000003,389637,5.332321,-65.55149,12.32391,0.867613103,0.345799842,0.902593358,0.56308979,0.77084519 QUAD,5000004,389638,4.332321,-35.55149,14.27251,0.429855813,0.882431001,0.15453015,0.710809946,0.918974895 QUAD,5000005,389639,5.332455,-75.55149,15.37321,0.738401857,0.289007109,0.59902178,0.824840285,0.571976301 QUAD,5000006,389640,4.332321,-65.55149,13.27221,0.102678011,0.581496802,0.785601755,0.415283869,0.817283531 QUAD,5000007,389641,3.332321,-45.55149,12.67351,0.363658748,0.803570041,0.70688413,0.459446702,0.330453157 [\/code] ex366.txt [code language=\"python\"] awk '{ print $1, $2, $3, $4}' FS=, OFS=, example.csv [\/code] variant: [code language=\"python\"] awk 'BEGIN { FS=OFS=\",\" } NF { print $1, $2, $3, $4 }' example.csv [\/code] output: [code language=\"python\"] Type,LCID,EID,Nx QUAD,5000001,389635,5.21044 QUAD,5000002,389636,4.332321\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":1905,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/07\/370-find-max-and-min-in-column-using-awk\/","url_meta":{"origin":1886,"position":5},"title":"#370 Find min and max in column using awk","author":"gantovnik","date":"2023-07-21","format":false,"excerpt":"example.csv [code language=\"python\"] Type,LCID,EID,Nx,Ny,Nxy,Mx,My,Mxy,Qyz,Qxz QUAD,5000001,389635,5.21044,0.9993295,3.184029,0.228490059,0.762438914,0.349926963,0.007402616,0.645762323 QUAD,5000002,389636,4.332321,-45.55149,10.13951,0.128933201,0.289045003,0.442976734,0.394346902,0.965424659 QUAD,5000003,389637,5.332321,-65.55149,12.32391,0.867613103,0.345799842,0.902593358,0.56308979,0.77084519 QUAD,5000004,389638,4.332321,-35.55149,14.27251,0.429855813,0.882431001,0.15453015,0.710809946,0.918974895 QUAD,5000005,389639,5.332455,-75.55149,15.37321,0.738401857,0.289007109,0.59902178,0.824840285,0.571976301 QUAD,5000006,389640,4.332321,-65.55149,13.27221,0.102678011,0.581496802,0.785601755,0.415283869,0.817283531 QUAD,5000007,389641,3.332321,-45.55149,12.67351,0.363658748,0.803570041,0.70688413,0.459446702,0.330453157 [\/code] ex370.txt [code language=\"python\"] awk 'NR==2 || $4 < min {min=$4} END{ print \"min=\" min}' FS=, OFS=, example.csv awk 'NR==2 || $4 > max {max=$4} END{ print \"max=\" max}' FS=, OFS=, example.csv [\/code] output: [code language=\"python\"] min=3.332321 max=50.332455 [\/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\/1886","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=1886"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1886\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=1886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=1886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=1886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}