Hello,
This is a problem I've worked on a while and can't figure out.
There is a file.txt
..some stuff..
[[Category:98 births]]
[[Category:2nd C. deaths]]
..some stuff..
The Awk program is trying to extract the year portion of the birth and death ("98: and "2nd C.") using the below technique
#!/bin/awk
@include filefuncs
BEGIN{
fp = readfile("file.txt")
birth = years(fp, "births")
death = years(fp, "deaths")
print "Birth = "birth
print "Death = "death
}
function years(fp, type ,a,b)
{
if(type == "births") {
if(match(fp,/\yCategory:[[:punct:]A-z0-9]* births\y/, a))
split(a[0],b,":|births")
return b[2]
}
if(type == "deaths") {
if(match(fp,/\yCategory:[[:punct:]A-z0-9]* deaths\y/, a))
split(a[0],b,":|deaths")
return b[2]
}
return -1
}
There are other ways to do it via the command line, but I need inside a function in a script using readfile().
The above code returns the correct birth year, but the death year is mangled because the regex is grabbing both the birth and death strings.
It works when not using the readfile() function, instead getline and this regex
match(article,"Category:[A-z0-9].* deaths", a)
The ".*" grabs everything to the end of the line and since readline makes the entire file a single line it grabs to the end of the "line" (file). That's why I'm using word boundary ("\y"), which works, but it doesn't work if there is a space in the data, such as the case here with the death string ("2nd C."). I tried adding "[:space:]" but that didn't work. I think this is solvable with the right regex but I'm out of ideas.