howdoesthiswork
howdoesthiswork

Reputation: 61

What does this code do? (awk)

here I have a part of my awk code to parse a file but the output is not 100% what I want.

match($0,/root=[^,]*/){
        n=split(substr($0,RSTART+5,RLENGTH-5),N,/:/)

My Problem is that I can not tell by 100% what this piece of code is exactly doing ... Can someone just tell me what this two lines exactly do?

EDIT: I just want to know what the code does so I can fix it myself, so please do not ask something like: how the file you parse looks like? ..

Upvotes: 0

Views: 69

Answers (1)

Etan Reisner
Etan Reisner

Reputation: 81012

match(s, r [, a])

Returns the position in s where the regular expression r occurs, or 0 if r is not present, and sets the values of RSTART and RLENGTH. Note that the argument order is the same as for the ~ operator: str ~ re. If array a is provided, a is cleared and then elements 1 through n are filled with the portions of s that match the corresponding parenthesized subexpression in r. The 0'th element of a contains the portion of s matched by the entire regular expression r. Subscripts a[n, "start"], and a[n, "length"] provide the starting index in the string and length respectively, of each matching substring.

substr(s, i [, n])

Returns the at most n-character substring of s starting at i. If n is omitted, the rest of s is used.

split(s, a [, r])

Splits the string s into the array a on the regular expression r, and returns the number of fields. If r is omitted, FS is used instead. The array a is cleared first. Splitting behaves identically to field splitting, described above.

So when match finds something that matches /root=[^,]*/ in the line ($0) it will return that position (non-zero integers are truth-y for awk) and the action will execute.

The action then uses RSTART and RLENGTH as set by match to get the substring of the line that matched (minus root= because of the +5/-5) and then splits that into the array N on : and saves the number of fields split into n.

That could probably be changed to match($0, /root=([^,]*)/, N) as the pattern and then use N[1,"start"] in the action instead of substr if you wanted.

Upvotes: 2

Related Questions