BASH_REMATCH, regular expressions

Since version 3, bash has an additional binary operator `=~' which is able to handle regular expressions. Every substring which is matched by the subexpression which is in parenthesis can be accessed from the BASH_REMATCH variable.

Here is a small script i use to get the latest freshmeat releases
exec 4<>/dev/tcp/
printf "%s\n" "GET /freshmeat/feeds/fm-releases-global">&4
while read line; do
[[ "$line" =~ "<title>(.*)</title>" ]] && 
printf "%s\n" "${BASH_REMATCH[1]}"
exec 4>&-

~$ ./ announcements (Global)
Copyright It! 2.1 (Default branch)
pflogx 0.80 (Default branch)
Nogame 0.1-rc01 (Default branch)
Weight Calculator 1.0.0 (Default branch)
MIME Email message class 2006.02.20 (Default branch)
Gif Disassembler 2.2.2 (Default branch)
Xapian and Omega 0.9.4 (Default branch)
Top Music 3.0 PR4.1 (Stable branch)
Silver Shield PM Control for Linux 2.1 (Default branch)
Linux DiskTool 1.6.0 (Default branch)
Glub Tech Secure FTP 2.5.8 (Default branch)
Hecl Programming Language 20060220 (Default branch)
Ghost In The Mail 0.5 (Default branch)
phpMyAdmin 2.8.0-rc1 (Development branch)
Wiz Solitaire 1.7 (Default branch)
DB Browser 1.6.0 (Default branch)
online-bookmarks 0.6.4 (Default branch)
fairly fast packet filter 1.5.0 (Default branch)
My Address Book contacts manager 0.9.5 (Default branch)
GNU Hosting Helper 1.58 (Default branch)
C++ Regex Engine 1.04 (Default branch)
rTorrent 0.4.5 (Default branch)
libTorrent 0.8.5 (Default branch)
Pure FTP Server 1.0.21 (Default branch)
Linux Multimedia Box 0.9.1 (Default branch)
PerlSendmail 2.0 (Default branch)
SSH Filesystem 1.5 (Default branch)
Simple scAnning Tool 1.2 (Default branch)
Roadsend PHP SiteManager 2.6.0 (Stable branch)


Anonymous said...

very usefull

esofthub said...

These are useful scripts.

Anonymous said...

Hmm, geht auch als Zwei- bzw. Ein- Zeiler ;-)

lynx -source | \
while read line; do [[ $line =~ \<title\>(.*?)\</title\> ]] && printf "%s\n" "${BASH_REMATCH[1]}"; done

Anonymous said...

nice script