awkgawkmawk

MAWK: Store match() in variable


I try to use MAWK where the match() built-in function doesn't have a third value for variable:

match($1, /9f7fde/) {
  substr($1, RSTART, RLENGTH);
}

See doc.

How can I store this output into a variable named var when later I want to construct my output like this?

EDIT2 - Complete example:

Input file structure:

<iframe src="https://vimeo.com/191081157" frameborder="0" height="481" width="608" scrolling="no"></iframe>|Random title|Uploader|fun|tag1,tag2,tag3
<iframe src="https://vimeo.com/212192268" frameborder="0" height="481" width="608" scrolling="no"></iframe>|Random title|Uploader|fun|tag1,tag2,tag3

parser.awk:

{
  Embed = $1;
  Title = $2;
  User = $3;
  Categories = $4;
  Tags = $5;
}

BEGIN {
  FS="|";
}

# Regexp without pattern matching for testing purposes
match(Embed, /191081157/) {
  Id = substr(Embed, RSTART, RLENGTH);
}

{
  print Id"\t"Title"\t"User"\t"Categories"\t"Tags;
}

Expected output:

191081157|Random title|Uploader|fun|tag1,tag2,tag3

I want to call the Id variable outside the match() function.

MAWK version:

mawk 1.3.4 20160930
Copyright 2008-2015,2016, Thomas E. Dickey
Copyright 1991-1996,2014, Michael D. Brennan

random-funcs:       srandom/random
regex-funcs:        internal
compiled limits:
sprintf buffer      8192
maximum-integer     2147483647

Solution

  • The obvious answer would seem to be

    match($1, /9f7fde/) { var = "9f7fde"; }
    

    But more general would be:

    match($1, /9f7fde/) { var = substr($1, RSTART, RLENGTH); }