{"id":684,"date":"2025-05-04T15:35:45","date_gmt":"2025-05-04T06:35:45","guid":{"rendered":"https:\/\/www.lapidem.pw\/wordpress\/?p=684"},"modified":"2025-05-04T15:54:20","modified_gmt":"2025-05-04T06:54:20","slug":"awk","status":"publish","type":"post","link":"https:\/\/www.lapidem.pw\/wordpress\/?p=684","title":{"rendered":"awk"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n######### 1.1 pattern action => if pattern then action\n######### 1.2 What is True\/False in awk?\n######### 0 or \"\" is False\n<strong>echo \"1 foo\" | awk '4'<\/strong>\n<em>1 foo<\/em>\n######### 1.3 Comparison, matching\necho \"2 foo\" | awk '{print $0 == \"foo\"}'\necho \"3 foo\" | awk '{print 0\"\"}'\necho \"4 foo\" | awk '0\"\"'\necho \"5 foo\" | awk '0{} END{print \"END\"}'\n######### $0 ~ and {print $0} can be omitted\necho \"6 foo\" | awk '\/foo\/'       ### 6 foo\necho \"7 foo\" | awk '$0 ~ \/foo\/'  ### 7 foo\necho \"8 foo\" | awk '$1 = \"bar\"'  ### bar foo\necho \"9 foo bar\" | awk '$1 = \"bar\" {print}'\necho \"10 a b c\" | awk '($2 = \"\"){} END{print \"END\"}'\necho \"11 a b c\" | awk '!($2 = \"\")'\n######### 1.5 variables\necho \"12 a b c\" | awk 'NF == 4'\necho \"13 a b c d\" | awk 'NF = 2' ##### 13 a\necho \"echo -e '\"'14 a\\n b\\n \\n\\n\\n\\n c\\n d\\n'\"' | awk 'NF' #### delete empty line\"\necho -e \"14 a\\n b\\n \\n\\n\\n\\n c\\n d\\n\" | awk 'NF' #### delete empty line\n######### 1.6 functions\n######## gsub => sed 's\/A\/B\/g'\necho \"15 ab cd ef gh\" | awk '$0 = gsub(\/ab\/, \"\") {print \"contained\"} '\necho \"16 ab cd ef gh\" | awk '! ($0 = gsub(\/ac\/, \"\")) {print \"not contained\"}'\necho \"17 ab cd ef gh\" | awk '$0 = gsub(\/ac\/, \"\") \"\"' #### print \"0\"\necho \"17 ab cd ef gh\" | awk 'split($0, array) {print array&#91;1], array&#91;4]}'\n######### 2 Record\n######### 2.1 row = record\n######### RS: Record Separator\n######### 2.2 NR: Number of Record\necho \"seq 1 10 | awk 'NR>5'\"\nseq 1 10 | awk 'NR>5'\necho \"seq 1 100 | tac | awk 'NR&lt;=5' |tac\"\nseq 1 100 | tac | awk 'NR&lt;=5' |tac\n######### 2.5 Range like sed\necho \"echo seq 1 100 | awk 'NR==10, NR==15'\"\nseq 1 100 | awk 'NR==10, NR==15'\necho \"seq 1 100 | sed -e '1,9d' -e '16,\\$d'\"\nseq 1 100 | sed -e '1,9d' -e '15,$d'\n######### 2.7 grep\necho \"seq 1 10 | awk '\/&#91;23]\/'\"\nseq 1 10 | awk '\/&#91;23]\/'\nseq 1 10 | awk '$0 ~ \/&#91;23]\/'\n######### 2.8 grep -v\necho \"seq 1 10 | awk '!\/&#91;3-8]\/'\"\nseq 1 10 | awk '!\/&#91;3-8]\/'\nseq 1 10 | awk '$0 !~\/&#91;3-8]\/'\n######### 2.9 getline\nseq 1 10 | awk 'BEGIN{ \n    while (getline &lt; \"-\" >0){\n    \t  print $0, \"NR = \", NR;\n    }\n#    close(\"-\");\n} '\n######### 3.1 Fields, only variable FS can be passed directly like -F\n######### other variables are passed via -v var=$VAR\necho \"a,  b,  c\" | awk -F',' '{print $2}'\nVAR=\"awk\"; echo \"a, b, c\" | awk -v var=$VAR '{print var, $2 }'\n######### 3.3 FS can use regular expression\ndate | awk -F'&#91; :\/]' '{print $0,\"(->$2, $3)\",$2, $3}'\necho \"a b c\" | awk '!($2 = \"\")' # awk '{$2 = \"\"; print}'\n######### 3.4 \n# , is OFS (Output Field Separator)\n# string can be concatenated with a spacen\necho \"a b c\" | awk '{i = 2; while(i &lt;= NF){a = a OFS $(i++); print a}}'\n######## 3.6 reconstruction of fields\necho \"a,b,c\" |awk -F',' -v OFS=':' '4'\necho \"a,b,c\" |awk -F',' -v OFS=':' '$1=$1'\n######## 4.1 substr, index !!! 1 origin language\necho \"abcde\" |awk '{print substr($0, index($0,\"b\"))}'\n######## 4.3 match\necho \"abcde\" | awk 'match($0, \/c.*\/)'\necho \"abcde\" | awk 'match($0, \/c.*\/){print substr( $0, RSTART, RLENGTH)}'\necho \"echo \\\"abcde\\\" | grep -o 'c.*'\"\necho \"abcde\" | grep -o 'c.*'\necho \"abcde\" | grep -o '.'\n######## 5.2 atan2(y,x)=theta\necho \"pi=\" | awk '{print $0, atan2(0,-1)}'\n######### gsub => sed 's\/A\/B\/g'\necho \"15 ab cd ef gh\" | awk '$0 = gsub(\/ab\/, \"\") {print \"contained\"} '\necho \"16 ab cd ef gh\" | awk '! ($0 = gsub(\/ac\/, \"\")) {print \"not contained\"}'\necho \"17 ab cd ef gh\" | awk '$0 = gsub(\/ac\/, \"\") \"\"' #### print \"0\"\necho \"17 ab cd ef gh\" | awk 'split($0, array) {print array&#91;1], array&#91;4]}'\n\n######### 2 Record\n######### 2.1 row = record\n######### RS: Record Separator\n######### 2.2 NR: Number of Record\necho \"seq 1 10 | awk 'NR>5'\"\nseq 1 10 | awk 'NR>5'\necho \"seq 1 100 | tac | awk 'NR&lt;=5' |tac\"\nseq 1 100 | tac | awk 'NR&lt;=5' |tac\n######### 2.5 Range like sed\necho \"echo seq 1 100 | awk 'NR==10, NR==15'\"\nseq 1 100 | awk 'NR==10, NR==15'\necho \"seq 1 100 | sed -e '1,9d' -e '16,\\$d'\"\nseq 1 100 | sed -e '1,9d' -e '15,$d'\n######### 2.7 grep\necho \"seq 1 10 | awk '\/&#91;23]\/'\"\nseq 1 10 | awk '\/&#91;23]\/'\nseq 1 10 | awk '$0 ~ \/&#91;23]\/'\n######### 2.8 grep -v\necho \"seq 1 10 | awk '!\/&#91;3-8]\/'\"\nseq 1 10 | awk '!\/&#91;3-8]\/'\nseq 1 10 | awk '$0 !~\/&#91;3-8]\/'\n######### 2.9 getline\nseq 1 10 | awk 'BEGIN{ \n    while (getline &lt; \"-\" >0){\n    \t  print $0, \"NR = \", NR;\n    }\n#    close(\"-\");\n} '\n######### 3.1 Fields, only variable FS can be passed directly like -F\n######### other variables are passed via -v var=$VAR\necho \"a,  b,  c\" | awk -F',' '{print $2}'\nVAR=\"awk\"; echo \"a, b, c\" | awk -v var=$VAR '{print var, $2 }'\n######### 3.3 FS can use regular expression\ndate | awk -F'&#91; :\/]' '{print $0,\"(->$2, $3)\",$2, $3}'\necho \"a b c\" | awk '!($2 = \"\")' # awk '{$2 = \"\"; print}'\n######### 3.4 \n# , is OFS (Output Field Separator)\n# string can be concatenated with a spacen\necho \"a b c\" | awk '{i = 2; while(i &lt;= NF){a = a OFS $(i++); print a}}'\n######## 3.6 reconstruction of fields\necho \"a,b,c\" |awk -F',' -v OFS=':' '4'\necho \"a,b,c\" |awk -F',' -v OFS=':' '$1=$1'\n######## 4.1 substr, index !!! 1 origin language\necho \"abcde\" |awk '{print substr($0, index($0,\"b\"))}'\n######## 4.3 match\necho \"abcde\" | awk 'match($0, \/c.*\/)'\necho \"abcde\" | awk 'match($0, \/c.*\/){print substr( $0, RSTART, RLENGTH)}'\necho \"abcde\" | grep -o 'c.*'\necho \"abcde\" | grep -o '.'\n######## 5.2 atan2(y,x)=theta\necho \"pi=\" | awk '{print $0, atan2(0,-1)}'\n\necho \"######## 6.5 array\"\necho -e \"aaa\\nbbb\\naaa\" | awk '!a&#91;$0]++ {print} a&#91;$0]{print \"a&#91;\" $0 \"]=\" a&#91;$0]  }'\n#echo -e \"aaa\\nbbb\\naaa\\naaa\\nbbb\\naaa\" | awk 'a&#91;$1]++ >=2{print} a&#91;$0]++ ==1 {print} {print \"---\"}'\necho 'echo  -e \"aaa\\nbbb\\naaa\" |sort|uniq -d'\necho  -e \"aaa\\nbbb\\naaa\" |sort|uniq -d\n\necho \"####### 8.1 systemtime(), strftime\"\nawk 'BEGIN {print strftime(\"%Y\/%m\/%d %H:%M:%S\", systime())}'\n\necho \"####### 8.2 gensub &amp; gsub\"\necho \"gensub demo: sub and gsub rewrite \\$0\":\necho \"sumomomo momonouchi\" | awk '{ sub(\/m\/,\"M\"); print }'\necho \"sumomomo momonouchi\" | awk '{ gsub(\/m\/,\"M\"); print }'\necho \"sumomomo momonouchi\" | awk '{ str=gensub(\/m\/,\"M\",\"g\",$0); print $0, str}'\necho \"sumomomo momonouchi\" | awk '{ str=gensub(\/m\/,\"M\",\"2\",$0); print $0, str}'\necho \"sumomomo momonouchi\" | awk '{ print gensub(\/(mo)(no)\/,\"\\\\1\\\"\\\\2\\\"\",\"g\",$0)}'\necho \"sumomomo momonouchi\" | sed 's\/\\(mo\\)\\(no\\)\/\\1\\\"\\2\\\"\/g'\n####### 8.3 |&amp;\necho \"a c b z e y f\" | awk '\n{\n\tcmd=\"sort\"; \n\tfor(i=1; i&lt;=NF; i++){print $i |&amp; cmd} \n\tclose( cmd, \"to\")\n\tstr=\"\";\n\twhile(cmd |&amp; getline > 0){\n\t\tstr = str \" \" $0;\n\t\tprint\n\t}\n\tclose(cmd);\n\tprint str;\n} '\nseq 1 10|rev|sort|xargs\n\necho \"####### 9.1 FPAT: Field Pattern, OFS: Output Field Separator\"\necho 'aaa, \"bbb, ccc\", ddd '\necho 'aaa, \"bbb, ccc\", ddd ' |awk -F, '{$1=$1;print $2}'\necho 'aaa, \"bbb, ccc\", ddd ' |awk -v FPAT='(\\\"&#91;^\\\"]+\\\")|(&#91;^,]+)' '{$1=$1;print $1,$2, $3, $4}'\necho 'aaa, \"bbb,ccc\", ddd ' |awk -v FPAT='(\\\"&#91;^\\\"]+\\\")|(&#91;^,]+)' '{print $1, $2}'\necho 'aaa, \"bbb, ccc\", ddd ' |awk -v FPAT='(&#91;^,]+)' '{OFS=\";\";print $1, $2, $3, $4}'\necho \"### cat -\"\nls -lg |awk '\/^-rwx\/{print $4}' | xargs |tr ' ' '+' |bc  | for i in `cat -`; do echo $i \"\/1000\"; done |bc|echo `cat -` \"kb\"\nls -lg |awk '\/^-rwx\/{print $4}' | xargs |tr ' ' '+' |bc| echo `cat -` \"\/1024\" |bc|echo `cat -` \"kb\"\n### \u6841\u533a\u5207\u308a\nprintf(\"# %\\047d %\\047d\/%\\047d\\n\", PMID, cnt, serial)\n### stdout flush\nif((matched % 100) == 0) printf(\".\"); system(\"\")\n### getline\ngetline query &lt; QY\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43,39],"tags":[],"class_list":["post-684","post","type-post","status-publish","format-standard","hentry","category-awk","category-shell-script"],"_links":{"self":[{"href":"https:\/\/www.lapidem.pw\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/684","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lapidem.pw\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lapidem.pw\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lapidem.pw\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lapidem.pw\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=684"}],"version-history":[{"count":4,"href":"https:\/\/www.lapidem.pw\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/684\/revisions"}],"predecessor-version":[{"id":689,"href":"https:\/\/www.lapidem.pw\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/684\/revisions\/689"}],"wp:attachment":[{"href":"https:\/\/www.lapidem.pw\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=684"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lapidem.pw\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=684"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lapidem.pw\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=684"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}