⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 awkcode.txt

📁 c programming pearls answer
💻 TXT
📖 第 1 页 / 共 5 页
字号:
percent               printf("%10.2f %5.1f\n", x[i], 100*x[i]/sum)percent     }histogram # histogramhistogram #   input:  numbers between 0 and 100histogram #   output: histogram of decileshistogram histogram     { x[int($1/10)]++ }histogram histogram END { for (i = 0; i < 10; i++)histogram           printf(" %2d - %2d: %3d %s\n",histogram               10*i, 10*i+9, x[i], rep(x[i],"*"))histogram       printf("100:      %3d %s\n", x[10], rep(x[10],"*"))histogram     }histogram histogram function rep(n,s,   t) {  # return string of n s'shistogram     while (n-- > 0)histogram         t = t shistogram     return thistogram }hist.sh awk 'hist.sh # generate random integershist.sh BEGIN { for (i = 1; i <= 200; i++)hist.sh             print int(101*rand())hist.sh       }hist.sh ' |hist.sh awk -f histogramhistans1.awk histans1.awk     { x[int($1/10)]++ }histans1.awk END { max = MAXSTARS = 25histans1.awk       for (i = 0; i <= 10; i++)histans1.awk           if (x[i] > max)histans1.awk               max = x[i]histans1.awk       for (i = 0; i <= 10; i++)histans1.awk           y[i] = x[i]/max * MAXSTARShistans1.awk       for (i = 0; i < 10; i++)histans1.awk           printf(" %2d - %2d: %3d %s\n",histans1.awk               10*i, 10*i+9, x[i], rep(y[i],"*"))histans1.awk       printf("100:      %3d %s\n", x[10], rep(y[10],"*"))histans1.awk     }histans1.awk histans1.awk function rep(n,s,   t) {  # return string of n s'shistans1.awk     while (n-- > 0)histans1.awk         t = t shistans1.awk     return thistans1.awk }sumcomma # sumcomma - add up numbers containing commassumcomma sumcomma     { gsub(/,/, ""); sum += $0 }sumcomma END { print sum }addcomma # addcomma - put commas in numbersaddcomma #   input:  a number per lineaddcomma #   output: the input number followed byaddcomma #      the number with commas and two decimal places addcomma addcomma     { printf("%-12s %20s\n", $0, addcomma($0)) }addcomma addcomma function addcomma(x,   num) {addcomma     if (x < 0)addcomma         return "-" addcomma(-x)addcomma     num = sprintf("%.2f", x)   # num is dddddd.ddaddcomma     while (num ~ /[0-9][0-9][0-9][0-9]/)addcomma         sub(/[0-9][0-9][0-9][,.]/, ",&", num)addcomma     return numaddcomma }addcomma.ans /^[+-]?[0-9][0-9]?[0-9]?(,[0-9][0-9][0-9])*$/ {addcomma.ans         gsub(/,/, "")addcomma.ans         sum += $0addcomma.ans         nextaddcomma.ans }addcomma.ans       { print "bad format:", $0 }addcomma.ans END   { print sum }addcomma.ans2 /^[+-]?[0-9][0-9]?[0-9]?(,[0-9][0-9][0-9])*([.][0-9]*)?$/ {addcomma.ans2         gsub(/,/, "")addcomma.ans2         sum += $0addcomma.ans2         nextaddcomma.ans2 }addcomma.ans2       { print "bad format:", $0}addcomma.ans2 END   { print sum }datecvt # date convert - convert mmddyy into yymmdd in $1datecvt datecvt { $1 = substr($1,5,2) substr($1,1,2) substr($1,3,2); print }date.data 013042 mary's birthdaydate.data 032772 mark's birthdaydate.data 052470 anniversarydate.data 061209 mother's birthdaydate.data 110175 elizabeth's birthdaydaynum function daynum(y, m, d,    days, i, n) {   # 1 == Jan 1, 1901daynum     split("31 28 31 30 31 30 31 31 30 31 30 31", days)daynum     # 365 days a year, plus one for each leap yeardaynum     n = (y-1901) * 365 + int((y-1901)/4)daynum     if (y % 4 == 0) # leap year from 1901 to 2099daynum         days[2]++daynum     for (i = 1; i < m; i++)daynum         n += days[i]daynum     return n + ddaynum }daynum     { print daynum($1, $2, $3) }nm.output file.o:nm.output 00000c80 T _addrootnm.output 00000b30 T _checkdevnm.output 00000a3c T _checkduplnm.output          U _chownnm.output          U _clientnm.output          U _closenm.output funmount.o:nm.output 00000000 T _funmountnm.output          U cerrornm.format # nm.format - add filename to each nm output linenm.format nm.format NF == 1 { file = $1 }nm.format NF == 2 { print file, $1, $2 }nm.format NF == 3 { print file, $2, $3 }prchecks # prchecks - print formatted checksprchecks #   input:  number \t amount \t payeeprchecks #   output: eight lines of text for preprinted check formsprchecks prchecks BEGIN {prchecks     FS = "\t"prchecks     dashes = sp45 = sprintf("%45s", " ")prchecks     gsub(/ /, "-", dashes)        # to protect the payeeprchecks     "date" | getline date         # get today's dateprchecks     split(date, d, " ")prchecks     date = d[2] " " d[3] ", " d[6]prchecks     initnum()    # set up tables for number conversionprchecks }prchecks NF != 3 || $2 >= 1000000 {        # illegal dataprchecks     printf("\nline %d illegal:\n%s\n\nVOID\nVOID\n\n\n", NR, $0)prchecks     next                          # no check printedprchecks }prchecks {   printf("\n")                  # nothing on line 1prchecks     printf("%s%s\n", sp45, $1)    # number, indented 45 spacesprchecks     printf("%s%s\n", sp45, date)  # date, indented 45 spacesprchecks     amt = sprintf("%.2f", $2)     # formatted amountprchecks     printf("Pay to %45.45s   $%s\n", $3 dashes, amt)  # line 4prchecks     printf("the sum of %s\n", numtowords(amt))        # line 5prchecks     printf("\n\n\n")              # lines 6, 7 and 8prchecks }prchecks prchecks function numtowords(n,   cents, dols) { # n has 2 decimal placesprchecks     cents = substr(n, length(n)-1, 2)prchecks     dols = substr(n, 1, length(n)-3)prchecks     if (dols == 0)prchecks         return "zero dollars and " cents " cents exactly"prchecks     return intowords(dols) " dollars and " cents " cents exactly"prchecks }prchecks prchecks function intowords(n) {prchecks     n = int(n)prchecks     if (n >= 1000)prchecks         return intowords(n/1000) " thousand " intowords(n%1000)prchecks     if (n >= 100)prchecks         return intowords(n/100) " hundred " intowords(n%100)prchecks     if (n >= 20)prchecks         return tens[int(n/10)] " " intowords(n%10)prchecks     return nums[n]prchecks }prchecks prchecks function initnum() {prchecks     split("one two three four five six seven eight nine " \prchecks           "ten eleven twelve thirteen fourteen fifteen " \prchecks           "sixteen seventeen eighteen nineteen", nums, " ")prchecks     split("ten twenty thirty forty fifty sixty " \prchecks           "seventy eighty ninety", tens, " ")prchecks }checkfix.ans # prchecks - print formatted checkscheckfix.ans #     input:  number \t amount \t payeecheckfix.ans #     output: eight lines of text for preprinted check formscheckfix.ans checkfix.ans BEGIN {checkfix.ans     FS = "\t"checkfix.ans     dashes = sp45 = sprintf("%45s", " ")checkfix.ans     gsub(/ /, "-", dashes)        # to protect the payeecheckfix.ans     "date" | getline date         # get today's datecheckfix.ans     split(date, d, " ")checkfix.ans     date = d[2] ". " d[3] ", " d[6]checkfix.ans     initnum()    # set up tables for number conversioncheckfix.ans }checkfix.ans NF != 3 {checkfix.ans     printf("\nrec %d has %d fields:\n|%s|\n\nVOID\nVOID\n\n\n",checkfix.ans               NR, NF, $0)checkfix.ans     nextcheckfix.ans }checkfix.ans {   printf("\n")                  # nothing on line 1checkfix.ans     printf("%s%s\n", sp45, $1)    # number, indented 45 spacescheckfix.ans     printf("%s%s\n", sp45, date)  # date, indented 45 spacescheckfix.ans     amt = sprintf("%.2f", $2)     # formatted amountcheckfix.ans     printf("Pay to %45.45s   $%s\n", $3 dashes, amt)  # line 4checkfix.ans     printf("the sum of %s\n", numtowords(amt))        # line 5checkfix.ans     printf("\n\n\n")              # lines 6, 7 and 8checkfix.ans }checkfix.ans function numtowords(n,   cents, dols, s) { # n has 2 decimal placescheckfix.ans     cents = substr(n, length(n)-1, 2)checkfix.ans     dols = substr(n, 1, length(n)-3)checkfix.ans     if (dols == 0)checkfix.ans         s = "zero dollars and " cents " cents exactly"checkfix.ans     elsecheckfix.ans         s = intowords(dols) " dollars and " cents " cents exactly"checkfix.ans     sub(/^one dollars/, "one dollar", s)checkfix.ans     gsub(/  +/, " ", s)checkfix.ans     return scheckfix.ans }checkfix.ans function intowords(n) {checkfix.ans     n = int(n)checkfix.ans     if (n >= 1000000)checkfix.ans         return("VOID")checkfix.ans     if (n >= 1000)checkfix.ans         return intowords(n/1000) " thousand " intowords(n%1000)checkfix.ans     if (n >= 100)checkfix.ans         return intowords(n/100) " hundred " intowords(n%100)checkfix.ans     if (n >= 20)checkfix.ans         return tens[int(n/10)] " " intowords(n%10)checkfix.ans     return nums[n]checkfix.ans }checkfix.ans function initnum() {checkfix.ans     split("one two three four five six seven eight nine " \checkfix.ans           "ten eleven twelve thirteen fourteen fifteen " \checkfix.ans           "sixteen seventeen eighteen nineteen", nums, " ")checkfix.ans     split("ten twenty thirty forty fifty sixty " \checkfix.ans           "seventy eighty ninety", tens, " ")checkfix.ans }colcheck # colcheck - check consistency of columnscolcheck #   input:  rows of numbers and stringscolcheck #   output: lines whose format differs from first linecolcheck colcheck NR == 1	{colcheck     nfld = NFcolcheck     for (i = 1; i <= NF; i++)colcheck        type[i] = isnum($i)colcheck }colcheck {   if (NF != nfld)colcheck        printf("line %d has %d fields instead of %d\n",colcheck           NR, NF, nfld)colcheck     for (i = 1; i <= NF; i++)colcheck        if (isnum($i) != type[i])colcheck           printf("field %d in line %d differs from line 1\n",colcheck              i, NR)colcheck }colcheck colcheck function isnum(n) { return n ~ /^[+-]?[0-9]+$/ }p12check # p12check - check input for alternating .P1/.P2 delimitersp12check p12check /^\.P1/ { if (p != 0)p12check               print ".P1 after .P1, line", NRp12check           p = 1p12check         }p12check /^\.P2/ { if (p != 1)p12check               print ".P2 with no preceding .P1, line", NRp12check           p = 0p12check         }p12check END     { if (p != 0) print "missing .P2 at end" }delim.ans BEGIN {delim.ans     expects["aa"] = "bb"delim.ans     expects["cc"] = "dd"delim.ans     expects["ee"] = "ff"delim.ans }delim.ans /^(aa|cc|ee)/ {delim.ans     if (p != "")delim.ans         print "line", NR, ": expected " pdelim.ans     p = expects[substr($0, 1, 2)]delim.ans }delim.ans /^(bb|dd|ff)/ {delim.ans     x = substr($0, 1, 2)delim.ans     if (p != x) {delim.ans         print "line", NR, ": saw " xdelim.ans         if (p)delim.ans             print ", expected", pdelim.ans     }delim.ans     p = ""delim.ans }delim.ans END {delim.ans     if (p != "")delim.ans         print "at end, missing", pdelim.ans }passwd # passwd - check password filepasswd passwd BEGIN {passwd     FS = ":" }passwd NF != 7 {passwd     printf("line %d, does not have 7 fields: %s\n", NR, $0) }passwd $1 ~ /[^A-Za-z0-9]/ {passwd     printf("line %d, nonalphanumeric user id: %s\n", NR, $0) }passwd $2 == "" {passwd     printf("line %d, no password: %s\n", NR, $0) }passwd $3 ~ /[^0-9]/ {passwd     printf("line %d, nonnumeric user id: %s\n", NR, $0) }passwd $4 ~ /[^0-9]/ {passwd     printf("line %d, nonnumeric group id: %s\n", NR, $0) }passwd $6 !~ /^\// {passwd     printf("line %d, invalid login directory: %s\n", NR, $0) }checkgen.data NF != 7			does not have 7 fieldscheckgen.data $1 ~ /[^A-Za-z0-9]/	nonalphanumeric user idcheckgen.data $2 == ""		no passwordcheckgen # checkgen - generate data-checking programcheckgen #     input:  expressions of the form: pattern tabs messagecheckgen #     output: program to print message when pattern matchescheckgen checkgen BEGIN { FS = "\t+" }checkgen { printf("%s {\n\tprintf(\"line %%d, %s: %%s\\n\",NR,$0) }\n",checkgen       $1, $2)checkgen }valid.ans BEGIN { FS = "\t" }valid.ans /^=/  { print substr($0, 2); next }valid.ans { printf("%s {\n\tprintf(\"line %%d, %s: %%s\\n\",NR,$0) }\n",valid.ans       $1, $2)valid.ans }compat # compat - check if awk program uses new built-in namescompat compat BEGIN { asplit("close system atan2 sin cos rand srand " \compat                "match sub gsub", fcns)compat         asplit("ARGC ARGV FNR RSTART RLENGTH SUBSEP", vars)compat         asplit("do delete function return", keys)compat       }compat compat       { line = $0 }compat compat /"/   { gsub(/"([^"]|\\")*"/, "", line) }     # remove strings,compat /\//  { gsub(/\/([^\/]|\\\/)+\//, "", line) } # reg exprs,compat /#/   { sub(/#.*/, "", line) }                # and commentscompat compat       { n = split(line, x, "[^A-Za-z0-9_]+")  # into wordscompat         for (i = 1; i <= n; i++) {compat             if (x[i] in fcns)	compat                 warn(x[i] " is now a built-in function")compat             if (x[i] in vars)compat                 warn(x[i] " is now a built-in variable")compat             if (x[i] in keys)compat                 warn(x[i] " is now a keyword")compat         }compat       }compat compat function asplit(str, arr) {  # make an assoc array from strcompat     n = split(str, temp)compat     for (i = 1; i <= n; i++)compat         arr[temp[i]]++compat     return ncompat }compat compat function warn(s) {compat     sub(/^[ \t]*/, "")compat     printf("file %s, line %d: %s\n\t%s\n", FILENAME, FNR, s, $0)compat }bundle # bundle - combine multiple files into onebundle bundle { print FILENAME, $0 }unbundle # unbundle - unpack a bundle into separate filesunbundle unbundle $1 != prev { close(prev); prev = $1 }unbundle            { print substr($0, index($0, " ") + 1) >$1 }addr.1 Adam Smithaddr.1 1234 Wall St., Apt. 5Caddr.1 New York, NY 10021addr.1 212 555-4321addr.1 addr.1 David W. Copperfieldaddr.1 221 Dickens Laneaddr.1 Monterey, CA 93940addr.1 408 555-0041addr.1 work phone 408 555-6532addr.1 Mary, birthday January 30addr.1 addr.1 Canadian Consulateaddr.1 555 Fifth Aveaddr.1 New York, NYaddr.1 212 586-2400ny1.awk BEGIN { RS = "" }ny1.awk /New York/ny2.awk BEGIN { RS = ""; ORS = "\n\n" }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -