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: ,

Welcome to #!/bin/bash

July 24th, 2009 subogero Comments off

This blog is about my adventures in the weird and wonderful world of Unix, Linux, GNU, cygwin, Ubuntu, the Bourne Again Shell, yacc, makefiles and similar terrible things.

I mean, who would not run for shelter when confronted with this:

$(MK_TMP)/%.dep: %.c
  @$(CC) $< $(CFLAGS1) $(INCDIRS) $(CDEFS) -Em 2>>$(LOG) \
  | sed -e '/\.\c/d'                                     \
        -e '\%$(INC_TMP)%d'                              \
        -e 's,\($*\).obj[ :]*,$(OBJ_TMP)/\1.o $@ :,g'    \
  | tr -d '\r' > $@ 2>$(@:.dep=.log)
Categories: posts Tags: