{"id":886,"date":"2021-02-09T19:52:27","date_gmt":"2021-02-10T03:52:27","guid":{"rendered":"http:\/\/gantovnik.com\/bio-tips\/?p=886"},"modified":"2021-02-09T19:52:27","modified_gmt":"2021-02-10T03:52:27","slug":"157-formatting-output-in-awk","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2021\/02\/157-formatting-output-in-awk\/","title":{"rendered":"#157 Formatting output in awk"},"content":{"rendered":"<p>#157 Formatting output in awk<\/p>\n<p>Without formatting, the command look like this<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nawk ' BEGIN {FS=&quot;:&quot;} { print $1,$3,$7 } ' \/etc\/passwd\r\n<\/pre>\n<p>The same command with formatting<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nawk ' BEGIN {FS=&quot;:&quot;} { printf &quot;%10s %4d %17s\\n&quot;,$1,$3,$7 } ' \/etc\/passwd\r\n<\/pre>\n<p>With the header information added to the BEGIN block:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nawk ' BEGIN {FS=&quot;:&quot; ; print &quot;%10s %4s %17s\\n&quot;,&quot;Name&quot;,&quot;UID&quot;,&quot;Shell&quot;} { printf &quot;%10s %4d %17s\\n&quot;,$1,$3,$7 } ' \/etc\/passwd\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>#157 Formatting output in awk Without formatting, the command look like this awk &#8216; BEGIN {FS=&quot;:&quot;} { print $1,$3,$7 } &#8216; \/etc\/passwd The same command with formatting awk &#8216; BEGIN {FS=&quot;:&quot;} { printf &quot;%10s %4d %17s\\n&quot;,$1,$3,$7 } &#8216; \/etc\/passwd With the header information added to the BEGIN block: awk &#8216; BEGIN {FS=&quot;:&quot; ; print &quot;%10s [&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,8,20],"tags":[],"class_list":["post-886","post","type-post","status-publish","format-standard","hentry","category-awk","category-bash","category-linux"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-ei","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":892,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/02\/159-awk-control-file\/","url_meta":{"origin":886,"position":0},"title":"#159 Awk control file","author":"gantovnik","date":"2021-02-09","format":false,"excerpt":"#159 Awk control file Create awk control file func1.awk [code language=\"python\"] function green(s) { printf \"\\033[1;32m\" s \"\\033[0m\\n\" } BEGIN { FS=\":\" green(\" Name: UID: Shell:\") } { printf \"%10s %4d %17s\\n\",$1,$3,$7 } [\/code] Command to run awk control file is [code language=\"python\"] awk -f func1.awk \/etc\/passwd [\/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":867,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/02\/156-filtering-content-from-files-using-awk\/","url_meta":{"origin":886,"position":1},"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":890,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/02\/158-filtering-content-from-files-based-on-field-value-using-awk\/","url_meta":{"origin":886,"position":2},"title":"#158 Filtering content from files based on field value using awk","author":"gantovnik","date":"2021-02-09","format":false,"excerpt":"#158 Filtering content from files based on field value using awk Request to print lines which has value in field #3 larger than 999: [code language=\"python\"] awk -F\":\" '$3 > 999 ' \/etc\/passwd [\/code] Request to print lines which has value in field #3 smaller than 101: [code language=\"python\"] awk\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":886,"position":3},"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":[]},{"id":1903,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/07\/368-performing-calculations-column-wise-using-awk\/","url_meta":{"origin":886,"position":4},"title":"#368 Performing calculations column-wise 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] ex368.txt [code language=\"python\"] awk 'NR==1{''} NR>1{{ SUM=SUM+$4 }} END { print \"Sum=\" SUM }' FS=, OFS=, example.csv [\/code] output: [code language=\"python\"] Sum=32.2045 [\/code] ex368.txt [code language=\"python\"] awk 'NR==1{''} NR>1{{ SUM=SUM+$4 }} END {print \"Mean=\" SUM\/(NR-1)}' FS=, OFS=, example.csv\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":886,"position":5},"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":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/886","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=886"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/886\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}