Archive

Posts Tagged ‘sed’

Curling

November 29th, 2010 subogero Comments off

I’m not referring to the most boring sport EVER. Instead, it’s a command line tool I’ve discovered recently: curl. You specify a URL on the command line (e.g. curl http://napirajz.hu), and it prints the contents to standard output.

Time to write my most popular website ever:

The business case:
Bloody multinational corporations filter the internet. One cannot access inspiring content like http://napirajz.hu. Let’s make it accessible.

The tools:
My OpenWrt router hanging on the internet. I’ll create a mirror site on it, refreshed every day at 5:00 (before breakfast), 9:00 (arriving at the office), 13:00 (after lunch) and 18:00 (before I get home).

The beloved cron daemon will do the timing. And the mirroring? On OpenWrt, one lacks the luxuries or Perl and even the cosy comfort of Bash. One has to make do with the BusyBox versions of the Almquist Shell (ash), curl, sed and wget.

#!/bin/ash
PICS=`curl 'http://napirajz.hu' 2>/dev/null \
| sed -nr 's,^[\t ]*<p><img src=\"([a-z0-9/_.]+)\".+$,http://napirajz.hu\1,p'`
rm -f *.jp*
echo '<html><head><title>subogero napi</title></head><body>'
echo '<h1 align="center">subogero napi</h1>'
echo '<hr>'
for i in $PICS; do
 wget $i
 echo $i \
 | sed -r 's,^.+/([a-z0-9_]+)(\.[jpeg]+)$,<p><a href=\"napi/\1\2\">\1</a>,'
done
echo '<hr>'
echo '(c) CC - Mer&eacute;nyi D&aacute;niel'
echo '<br>Eredeti: h t t p : / / n a p i r a j z . h u'
echo '<p>Friss&iacute;tve:'
date "+%Y.%m.%d %H:%M"
echo '</body></html>'

See the result at http://subogero.dyndns.org/git/napi/

Categories: Uncategorized Tags: , , , , ,

Seduced by sed

July 25th, 2009 subogero Comments off

So I started searching the internet about the usage of sed, the ultimate stream editor. I found the number one site about the Awful Truth. If you have any question about sed, you’ll find the answer here. My favourites so far:

Simple replacement of “Windows” to “Linux”, sorry “foo” to “bar” with the magic s (substitution), go for all occurrences this time with flag g (global).

sed -e 's/foo/bar/g'

Use any other delimiter instead of slash, in this case the comma, if slash is used in any pattern.
Keep a part of a matched string defined by the brakets \( \) and using \1.
Let’s convert “c:” to “/cygdrive/c”.

sed -e 's, \([A-Za-z]\):, /cygdrive/\1,g'

You will also notice the presence of the dreaded regular expression above. I also learned that sed is a so-called filter. In streams the standard input, so sed takes the text you type, and writes the result to the terminal window.

And now comes the best part, at least for a rookie like me, the redirection wizardry. You can redirect sed’s input or output to any other file, or even any other program’s standard input or output.

For instance let’s copy the contents of foo.txt to bar.txt, but will all occurrences of “foo” replaced with “bar”.

sed -e 's/foo/bar/g' <foo.txt >bar.txt

Same operation but replace in the same file, foo.txt.

sed -e 's/foo/bar/g' -i foo.txt

And using the pipe and the command-quote you can do the same between environment variables, as well. The pipe “|” connects the output of the program on its left to the input of the one on its right. And the command quote ` ` redirects the final output to a variable.

BAR=`echo $FOO | sed -e 's/foo/bar/g'`

I think it’s obvious by now that I’m infected. I use the words “foo” and “bar”.

Categories: posts Tags:

cygwin – How it All Started

July 25th, 2009 subogero Comments off

At the very respectable company where I spend my days, we’ve been using a mysterious cygwin shell for building software projects. Don’t ask me why. I’d been wondering what the bash2.02> prompt was supposed to mean. Sometimes I overheard sentences about how good the GNU make was, or how cool it was to pipe sed into tr, but it’d be overstating the facts that I understood them.

Considering myself an embedded software wizard? Check.
Understanding our makefile? Nope.

Until the day came, when upgrading to the newest version of cygwin was proposed to achieve compatibility with a code-checking tool. And the loser to do this job was yours truly. So I upgraded and, guess what, our make ceased to work. After consulting some gurus in the office and the internet it turned out that cygwin had stopped supporting Win32 path formats altogether. Fantastic.

My beloved drive letters (C:\) were blown out of the window.
In came a weird /cygdrive/c/ format.
In case of a Unix-hacker, the terms root directory and mount-point spring to the lips.

It was suggested that I could fix this with the bloody sed in a matter of a few seconds, thank you very much.

And so it started…

Categories: posts Tags: ,