Article View: comp.lang.awk
Article #1News Headlines Using Awk
From: mike
Date: Tue, 27 Oct 2026 13:44
Date: Tue, 27 Oct 2026 13:44
116 lines
3842 bytes
3842 bytes
#!/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
Technical Details
Message-ID:
<11bq9ro$1hub6$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailShow Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: mike <mike@porkchop.nul>
Newsgroups: comp.lang.awk
Subject: News Headlines Using Awk
Date: Tue, 27 Oct 2026 13:44:27 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 116
Message-ID: <11bq9ro$1hub6$1@dont-email.me>
Injection-Date: Tue, 27 Oct 2026 13:44:27 +0000 (UTC)
Injection-Info: dont-email.me;
logging-data="1636710"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/qda/LNZABVNdmWn+Di9nI"; posting-host="e71456af46998bc042e0237c36fc68b9"
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (Linux/6.1.0-52-amd64 (x86_64))
Cancel-Lock: sha1:t2h5cQra749m+ABJ5Q0J/x78tRQ= sha256:vl5GTePtPBuE/S4OmNzPIMPALm4F1iCfonfzWE1ExtY=
sha1:hFH46I3b167Y3Gmga3veRlgXCWQ= sha256:38tBG1b6+eCec8m1bz4m9JR/OS3D89rx5ySRjdgdyX0=