• News Headlines Using Awk

    From mike@mike@porkchop.nul to comp.lang.awk on Tue Oct 27 13:44:27 2026
    From Newsgroup: comp.lang.awk

    #!/bin/sh

    <<NOTES

    Quick & dirty awk processing to grap n number of headlines
    from Google News & display each headline as a clickable link
    (if your terminal emulator supoorts clickable urls...)

    2026 Michael Sanders

    Dependiacies...

    active xsession & mouse
    /bin/sh
    date
    awk
    wget
    read
    browser
    printf

    Beware wordwrap in this post & have fun!

    NOTES

    PAD=1 # leading spaces for all printed lines
    NUM=10 # number of headlines to display
    MAX=60 # max char length for headlines

    [ -n "$DISPLAY" ] && printf "\033]0;Google News Brief\007"

    indent=""
    _i=0
    while [ "$_i" -lt "$PAD" ]; do
    indent="$indent "
    _i=$((_i + 1))
    done

    AGN="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
    DTE=$(date +'%b %d, %Y') URL="https://news.google.com/rss?hl=en-US&gl=US&ceid=US:en"

    # fetch google news feed using wget
    RSS=$(wget -qO- --header="User-Agent: $AGN" "$URL" 2>/dev/null)

    clear

    [ -z "$RSS" ] && {
    echo "Error: Failed to fetch Google News URL feed."
    exit 1
    }

    echo "$RSS" | awk -v indent_str="$indent" -v limit="$NUM" -v max_len="$MAX" -v curr_date="$DTE" '
    BEGIN {
    RS = "</item>";
    indent = indent_str;
    count = 0;
    }
    {
    # extract <title>
    t_start = index($0, "<title>");
    title = "";
    if (t_start > 0) {
    sub_str = substr($0, t_start + 7);
    t_end = index(sub_str, "</title>");
    if (t_end > 0) {
    title = substr(sub_str, 1, t_end - 1);
    gsub(/<!\[CDATA\[|\]\]>/, "", title);
    gsub(/^[ \t]+|[ \t]+$/, "", title);
    }
    }

    # extract <link>
    l_start = index($0, "<link>");
    link = "";
    if (l_start > 0) {
    sub_str = substr($0, l_start + 6);
    l_end = index(sub_str, "</link>");
    if (l_end > 0) {
    link = substr(sub_str, 1, l_end - 1);
    gsub(/<!\[CDATA\[|\]\]>/, "", link);
    gsub(/^[ \t]+|[ \t]+$/, "", link);
    }
    }

    # skip feed metadata / intro titles
    if (title != "" && title !~ /Google News/ && title !~ /^Top stories/) {
    count++;
    titles[count] = title;
    links[count] = link;
    if (count >= limit) exit;
    }
    }
    END {
    printf "\n%sGoogle News Brief: %s\n\n", indent, curr_date;

    # print numbered headlines with truncation & inline links
    for (i = 1; i <= count; i++) {
    t = titles[i];
    if (length(t) > max_len) {
    t = substr(t, 1, max_len - 3) "...";
    }

    u = "<\033]8;;" links[i] "\033\\more\033]8;;\033\\>";
    printf "%s%2d. %s %s\n", indent, i, t, u;
    }
    }'

    read -p "
    ${indent}CTRL+Click to open a link or ENTER to exit..." dummy

    # eof
    --
    :wq
    Mike Sanders
    --- Synchronet 3.22a-Linux NewsLink 1.2