Adler32Checksum
As documented in the [[http://en.wikipedia.org/wiki/Adler-32 | Adler-32 Wikipedia article]]. {{{ awk . . . while (++i <= len) { b += (a += ord[substr(str,i,1)]) # This should bottleneck the performance . . . these # modulus operations with a test-and-substract # method could run faster, but I simply couldn't . . . i in order to compute # the checksum of a substring; hacker beware! } } return a + (2^16 * b) . . .
4K - last updated 2008-12-31 12:05 UTC by pgas
AwkDc
This is an awk implementation of the [http://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html|dc] . . . /^_?[0-9]+$/) { if (op ~ /^_/) { sig = -1 num = (substr(op, 2) + 0) } else { sig = 1 num = (op + 0) . . .
12K - last updated 2008-07-17 14:47 UTC by gnomon
AwkGuide
** Work in Progress ** {{{ import from Mark Hobley's wiki }}} <toc> ---- == Overview * [[Overview]] . . . [[string manipulation function]]s * [[sub]] * [[substr]]ings * [[symbol]]s * [[table]]s * [[case conversion|tolower]] . . .
4K - last updated 2011-08-14 15:57 UTC by markhobley
AwkOnWindowsHowto
AwkOnWindowsHowto\\ rough cut - needs edit {{{ 3) awk command line switches/usage from a win32 cmd.exe . . . 0.20 i = index(s,x); 0.60 0.57 0.71 1.01 i = substr(s,32); 0.35 0.22 0.23 0.90 i = tolower(s); 0.15 . . .
3K - last updated 2010-10-24 07:44 UTC by g0pher
AwkTips
<toc> ---- == Be idiomatic! In this paragraph, we give some hints on how to write more idiomatic . . . where $0=="0" match($0,/[^,]*,| *"[^"]*" *,/); f=substr($0,RSTART,RLENGTH); # save what matched in sf . . . extra stuff print "Field " ++c " is " f; $0=substr($0,RLENGTH+1); # "consume" what matched } }}} . . .
31K - last updated 2009-03-24 16:54 UTC by waldner
Backref2
The usual (and correct) answer for backreferences in awk (for example, the answer you can get on #awk . . . and awk) require that you have only one matching substring to extract per line. * Third answer: see . . .
2K - last updated 2009-03-26 20:24 UTC by waldner
Backreferences
The usual (and correct) answer for backreferences in awk (for example, the answer you can get on #awk . . . and awk) require that you have only one matching substring to extract per line. For the same purpose, . . . to assign a regexp to RS to "pull out" substrings from the input (and without the limitation . . .
3K - last updated 2010-12-30 06:53 UTC by ppp089210039107.dsl.hol.gr
ConcatenationPrecedence
You'd expect `6 -22', but you get `6-22'. It's because the `" " -22' is grouped first, as a substraction . . .
1K - last updated 2008-12-15 12:37 UTC by pgas
CookBook
<toc> ---- Just started, the aim of this page is to collect small pieces of code that illustrate . . . answer]] ---- == How to find the last matching substring on a line for a given regular expression . . .
1K - last updated 2009-04-14 09:03 UTC by 216.162.199.202
FindAllIndices
Sometimes it is useful to find the index of every occurrence of a given character in a string. Let's . . . j) { for (i = 1; i <= length(str); i++) if (substr(str, i, 1) == chr) arr[++j] = i return j } }}} . . .
2K - last updated 2011-07-05 10:53 UTC by pgas
FindAllMatches
This bit of code will match all non-overlapping instances of a given pattern in a string and will store . . . while (match(str, re) > 0) { arr[++j] = substr(str, RSTART, RLENGTH) # start[j]=RSTART+eaten . . . in the string # eaten+=(RSTART+RLENGTH-1) str = substr(str, RSTART+RLENGTH) } RSTART = a; RLENGTH = . . .
2K - last updated 2009-03-24 09:46 UTC by waldner
GeneralizedTextReplacement
Some times people need to recode in awk something they were previously doing with sed or another tool. . . . be replaced). The idea is to find all matching substrings in the original text (the lhs of the s/pattern/replacement/ . . . match i-1 (or beginning of string) and i psep=substr(orgstr,last,start[j]-last) last=start[j]+length(mtch[j]) . . . trailer (what is after mtch[n]) newstr=newstr substr(orgstr,last) return newstr } # main body of . . . elements of the array mtch[] to break them into substring corresponding to the capture groups you would . . .
6K - last updated 2009-03-26 20:38 UTC by waldner
index
== Usage == === index [ STRING, SUBSTRING ] === The index [[function]] is used to locate the first occurrence . . . of a [[substring]] within a [[string]]. If the [[substring]] . . . of the [[string]] matches that of the [[substring]]. . . .
1K - last updated 2011-06-09 20:36 UTC by markhobley
LastMatchOnLine
The function 'match' sets RSTART to the index of each match and RLENGTH to its length, and returns 0 . . . result) { while(match(line, re)) { result = substr(line, RSTART, RLENGTH) line = substr(line, RSTART+RLENGTH) . . .
1K - last updated 2009-04-14 22:07 UTC by 216.162.199.202
LevenshteinEditDistance
The [http://en.wikipedia.org/wiki/Levenshtein_distance|Levenshtein edit distance] calculation is useful . . . == "") { return length(str1 str2) } else if (substr(str1, 1, 1) == substr(str2, 1, 1)) { a = 2 while . . . (substr(str1, a, 1) == substr(str2, a, 1)) a++ return . . . levdist(substr(str1, a), substr(str2, a)) } else if (substr(str1, . . . l1=length(str1), 1) == substr(str2, l2=length(str2), 1)) { b = 1 while (substr(str1, . . .
6K - last updated 2010-04-16 10:19 UTC by pgas
LshalOutputParser
=== Problem Specification 10:50 <Thanatermesis> i want to obtain the model of the disk, for example, . . . last on the line if (RLENGTH > 0) { type = substr($2, RSTART+1, RLENGTH-2) # offsets to remove . . . parentheses val = trim(substr($2, 1, (length($2) - RLENGTH))) # remove type . . .
3K - last updated 2008-06-27 16:22 UTC by gnomon
PrimeNumberSieve
This is the standard sieve of Eratosthenes implemented in portable awk. The running time of the bare . . . the spec). Arrays, numbered fields and the {{{substr()}}} built-in are the only facilities in awk . . .
4K - last updated 2008-06-19 05:56 UTC by gnomon
RangeOfFields
Printing a range of fields - all fields but the first, for examples, or fields 3 through 8 - is a surprisingly . . . line. You can remove the front space with {{{ $0=substr($0, 1+length(OFS) * 2) }}}, another possibility . . . don't exist. ==== Function using match() and substr() This approach uses match() to get the position . . . and length of each field separator, then substr() to either trim it from the beginning or add . . . i<start; i++) { if (match(str, re)) { str = substr(str, RSTART + RLENGTH); # there's no FS left, . . .
10K - last updated 2015-09-08 09:52 UTC by pgas
Recursively Include C headers
=== Problem Specification i want to generate one big .hpp file out of multiple .hpp files and want to . . . /^[ \t]*#include[ \ ]*"[^"]+"/)) { f = substr(line, RSTART, RLENGTH) sub(/^[ \t]*#include[ . . .
1K - last updated 2011-12-11 05:33 UTC by pgas
reverse
The [[awk]] extraction and reporting language does not provide a [[builtin]] function for reversing strings. . . . = "" for (l = length(s); l > 0 ; l--) { c = substr(s, l, 1) revs = revs c } return revs } # Call . . .
1K - last updated 2011-06-25 05:11 UTC by pgas
SedFAQ
<toc> ---- == I have a line like "abdcgfjeuPATTERNfjfhghj", I want to get the PATTERN part, why . . . most once per line. So, pulling out the matching substring is easily accomplished with this code: {{{sed . . . }}} Once we have this, this code removes all the substrings in even positions (2nd, 4th, etc.): {{{ . . . }}} If we first remove the very first substring (and the last), the same code will then remove . . . all the odd substrings: {{{ sed s/^[^_]*_// s/_[^_]*$// s/_[^_]*_//g . . .
34K - last updated 2010-02-14 17:56 UTC by waldner
SimpleCalc
Simple 4 operations calculator ... {{{ awk # usage: print calc("5 * (1 + 2) * 5 + 7") # reads and update . . . || match(s[0], /^[-+*/]/)) { t = substr(s[0], RSTART, RLENGTH) s[0] = substr(s[0], RLENGTH+1) . . .
2K - last updated 2015-09-10 06:14 UTC by pgas
SolarisNawk
I want to use the tolower() function with {{{SunOS}}} nawk, but all I get is {{{ nawk: calling undefined . . . or write your own function to do it using index, substr, and gsub. An example of such a function is . . .
1K - last updated 2008-11-24 08:24 UTC by pgas
SolarisNawkJapanese
I want to use the tolower() function with {{{SunOS}}} nawk, but all I get is {{{ nawk: calling undefined . . . or write your own function to do it using index, substr, and gsub. An example of such a function is . . .
1K - last updated 2008-11-24 08:36 UTC by pgas
special variable
Some [[variable]] names have a special meaning to the awk interpreter. == List of special variables == . . . provides the length of the currently matched substring | [[RS]] | A control variable that specifies . . . the start index of the currently matched substring | [[SUBSEP]] | A control variable that specifies . . .
2K - last updated 2011-06-25 05:46 UTC by pgas
SplitIntoChars
In portable POSIX awk, the only way to do this is to use substr to pull out each character, one by one. . . .
1K - last updated 2008-11-24 08:20 UTC by pgas
SplitIntoCharsJapanese
In portable POSIX awk, the only way to do this is to use substr to pull out each character, one by one. . . .
1K - last updated 2008-11-24 08:35 UTC by pgas
string manipulation
* [[case conversion]] * [[length|Determine the length of a string]] * [[index|Determine the position . . . of a substring within a string]] * [[substr|Removing the . . . a string]] * [[reverse|Reverse a string]] * [[substr|Splicing a string at arbitary positions]] * . . .
1K - last updated 2011-07-04 22:12 UTC by markhobley
string manipulation function
The [[awk]] programming language provides a set of [[builtin]] functions for performing various string . . . * [[split]] * [[sprintf]] * [[sub]] * [[substr]] * [[case conversion|tolower]] * [[case conversion|toupper]] . . .
1K - last updated 2011-06-07 00:35 UTC by markhobley
substr
=== Removing the first and last characters from a string === The following [[script]] demonstrates how . . . the [[substr]] function and [[length]] functions can be combined . . . {{{ awk BEGIN { mystring="knights" print substr(mystring,2) # remove the first letter print . . . substr(mystring,1,length(mystring)-1) # remove the . . . last character print substr(mystring,2,length(mystring)-2) # remove both . . .
1K - last updated 2011-06-25 05:10 UTC by pgas
text.2.wiki.awk
#: C:\#\awk\lib\text.2.wiki.awk\\ . . . s = substr($0, 1, length($0) - 1);\\ . . . s = substr(s, 1, length(s) - 2);\\ . . . j && j < k) s = substr(s, 1, j - 1) "\377" substr(s, j + 1);\\ . . .
11K - last updated 2012-09-24 14:55 UTC by g0ph3r
text 2 wiki.awk
{{{ #: C:\#\lib\awk\utl\text_2_wiki.awk #: 2012-09-15 23:40:11 #:rod.t_2012 #: This file is http://awk.freeshell.org/text_2_wiki.awk . . . ~ /\\\\$/) { s = $0; } else if($0 ~ /`$/) { s = substr($0, 1, length($0) - 1); } else s = $0 "\\\\"; . . .
2K - last updated 2012-09-16 09:12 UTC by g0ph3r
tilde
== Substring containment operator == The tilde [[symbol]] can be used as a containment operator providing . . .
1K - last updated 2011-05-19 19:04 UTC by markhobley
tracert.awk
{{{ awk #:: C:\_\_u\tracert.awk #: 2014-07-06 19:47:02 #:: rod.t_2014 #:: extract basename for tracert . . . match(s, /([a-z0-9-]+[.])+[a-z0-9-]+/); s = substr(s, RSTART, RLENGTH); s = (_cmd)s; print(" cmd . . .
4K - last updated 2014-07-07 06:15 UTC by 212.205.56.222
34 pages found.