Tag Archive: sed


Sed is a very powerful tool when used correctly but can also be a source of frusteration at times.  One problem with it shows up when you are trying to identify and replace non-printable characters.  Luckily sed has a nice command line switch that will help you debug certain problems with sed.

When you just use cat to display a file, you’ll see it as you are normally used to but this isn’t how sed sees files.

cat hello.c

When you use the -n ‘l’, you’ll see the file how sed sees it.  (It’s a lower case “L” in the single quotes)

sed -n ‘l’ hello.c

Now you can see the new line characters at the end of the lines and the tabs that proceed some of the lines.

I took a closer look at the Easy Wifi Configurator script implemented on Mozzwald’s Ubuntu for the Zipit.  There are a couple trivial differences between Debian, Iz2s and Ubuntu for the Zipit and the setup-wifi.sh script tries to account for them all.  The long and short of it for now is that you need to change the line with the gawk statement to use awk instead.  That worked for me at least for the short term. There is also a broken sed statement which needs some attention but that will get worked out later I’m sure.

This is a nifty trick you can use to impress your friends.  Imagine you have some smallish program that you want to wrap up into a single file for simplicity.  Part of final output perhaps relies on a clever bit of shell scripting and another part relies on a binary of some sort that you need to get the job done.  Here’s a way to wrap it all up in a tidy little package:

#!/bin/sh
grep SRC: $0 | grep -v “grep SRC:” | sed -e s/SRC://g > /tmp/hello.c
gcc -o hello /tmp/hello.c
./hello
exit
SRC: #include <stdio.h>
SRC: int main(int argc, char *argv[])
SRC: {
SRC: printf(“Hello World\n”);
SRC: return 0;
SRC: }

Everything below exit is completely ignored by your shell.  The grep statement goes through the shell script line by line and returns the all the lines starting with “SRC: ” to sed.  Sed uses a simple regex to strip the “SRC: ” off your source code and sends the output to a file in your /tmp directory.  Then the file is compiled and ran.  You could go further and delete the source file out of the /tmp directory if you like or don’t bother.

Powered by WordPress. Theme: Motion by 85ideas.