tech-pkg archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: help with awk





On Fri, 11 Nov 2022 at 13:09, Jason Bowen <jbowen%infinitecactus.com@localhost> wrote:
Anything not in a BEGIN{} or END{} block will be applied to each line of
the input.

It also seems like you're getting a bit mixed up with shell vs awk
variables. With awk, $i is field i of the input. If you want to use the
value of a variable named i, like in your loop, then just write i
without a dollar sign.

Also, in your for loop you're already grabbing individual fields, so
there's no need to split the line: $i will be the ith field of the input
line.

Does

     BEGIN { print "# $NetBSD$"; }
     /(^|\n)source =/ {
         for (i = 1; i <= NF; i++) {
            sub("\"","")
            sub(",","")
            if ($i ~ /^https/)
               name=$i
            if ($i ~ /[a-f0-9]{40}/)
               vers=$i
         }
     }
     END { print "DISTFILES+=\t" name "-" vers ".tar.gz" }

do what you want for DISTFILES and give you enough to go on for SITES.?

> Given the following line(s) of a file as an example:
>
> source = { git = "https://github.com/Beaglefoot/tree-sitter-awk", rev = "a799bc5da7c2a84bc9a06ba5f3540cf1191e4ee3" }
>
> and this awk script:
>
> BEGIN { print "# $Net" "BSD$"; }
> /(^|\n)source =/ {
>          for (i = 1; i <= NF; i++) {
>                  sub("\"","")
>                  sub(",","")
>                  split($i, f, " ")
>                  print "field f["$i"]"
>                  if (f[$i] ~ /^https/)
>                          name=f[6]
>                  if (f[$i] ~ /[a-f0-9]{40}/)
>                          vers=f[9]
>                  print name
>                  print vers
>          }
>          print "DISTFILES+=\t" name "-" vers ".tar.gz"
> }
> # SITES.name-vers.tar.gz= -url/archive/vers.tar.gz
> #/^source = {/ { print "DISTFILES+=\t" name "-" vers ".tar.gz"; }
>
>
> what do I fix to correctly output
> DISTFILES+=   name-vers.tar.gz
> SITES.name-vers.tar.gz= -url/archive/vers.tar.gz
>
> where name is the last path part of url?
> I know I'm still missing filtering the name from the url, and basically
> everything else, as it's been too long since I had to work in awk.
>
>
> This is to spare myself or whoever updates editor/helix to do the same
> copy-pasting 30+ minutes process again and again with every new release.

Fairly sure there's a cleaner way with awk regexps, but this works in a q'n'd way (I think) for the data you kind of inferred above:

[2022/11/11 Fri 14:17:14] agc@netbsd-002 ~.../nikita [2893] > cat m.sh
#! /bin/sh

awk '
BEGIN { print "$Net" "BSD$" }
/^source/ {
        gsub("[\",]", "")
        fc = split($6, fv, "/")
        printf("DISTFILES+=\t%s-%s.tar.gz\n", fv[fc], $9)
}' < in
[2022/11/11 Fri 14:17:17] agc@netbsd-002 ~.../nikita [2894] > cat in
source = { git = "https://github.com/Beaglefoot/tree-sitter-awk", rev = "a799bc5da7c2a84bc9a06ba5f3540cf1191e4ee3" }
[2022/11/11 Fri 14:17:20] agc@netbsd-002 ~.../nikita [2895] > ./m.sh
$NetBSD$
DISTFILES+=     tree-sitter-awk-a799bc5da7c2a84bc9a06ba5f3540cf1191e4ee3.tar.gz
[2022/11/11 Fri 14:17:24] agc@netbsd-002 ~.../nikita [2896] >

No error checking and assumes fields are kinda constant

Best,
Alistair


Home | Main Index | Thread Index | Old Index