Comment

Comment by j0e1

Here is an Awk program (not a one-liner) that extracted select snippets of text from lots of docs and worked almost 10x faster than a grep equivalent:

   awk 'BEGIN { ORS=" "} { print $0 }' $2 | awk --re-interval -v pat="$1" '
	{
            cut_content = substr($0, 1, match($0, "==== Refs"))
            orig_content = substr($0, 1, match($0, "==== Refs"))
            idx = 0
            regex = "(^|[^a-zA-Z]{1})" pat "([^a-zA-Z]{1}|$)"
            while (match(cut_content, regex)) {
              if (RLENGTH > 0) {
                 prot = substr(cut_content, RSTART-1, RLENGTH+1)
                 gsub(/[:punct: ]$/, "", prot)
                 gsub(/^[:punct: ]/, "", prot)

                 print prot, "\t", substr(orig_content, idx+RSTART-400, RLENGTH+800)
                 idx += (RSTART + RLENGTH -1)
                 cut_content = substr(cut_content, RSTART+RLENGTH)
              }
	    }
        }'
parent j0e1 8 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton