Posts Tagged ‘linux’

Cleaning source code with sed

Recently a ticket came across my desk to remove references to Google Plus from a set of email templates our team maintains. With dozens of email templates, I wanted to stretch my command line muscles and see if I could do it all at once.

macOS includes sed, the stream editor, and for this project I’m going to use a very simple set of commands.

Delete lines that match

d is for delete. To use this command, you have a regex, followed by the letter d. It will delete any line that matches.

For example:

sed -i.bak '/googlePlusIcon/d' someFile.txt

Substitute lines

s is for substitute. Think of it like “find and replace” except we are going to find something everywhere. There are 2 parts, a regex to match, and a regex

sed -i.bak 's/googlePlusIcon/twitterIcon/g' someFile.txt

Doing multiple files at once

You can do multiple files at once by combining find with sed. Find syntax is as follows:

find path expression

path is the scope where find will… find things. expression is a set of options to control what we match. For example, -name is to match on the filename.

Another use of expressions is to do some operation. -exec is for executing another command. In this case we want to execute sed. So we type out the sed command from above, except with a few changes. There are curly braces {} which tell find to put the filename of the match in there. There is also the \; which tells find where the -exec expression is ending.

find . -name '*.txt' -exec sed -i.bak 's/googlePlusIcon/twitterIcon/g' {} \;

One quirk about find + sed is that it likes to add newlines to the end of your files. If you are using source control like git, this may cause the diff to be slightly larger than you expected.

3D graphics on Linux

As I mentioned in my post about free software, one of the problems with my current Ubuntu installation is my use of a non-free graphics driver. I would prefer to find a way to use free software and still have some hardware acceleration support, e.g. for compiz and for video playback. (I found a good tutorial on how to fix video tearing.)

As a side note, I have a free graphics driver with hardware acceleration working on my Eee 900. This is mainly because the eee pc has Intel integrated graphics. On the other hand, its performance is nothing to write home about.

The target system is an HP Compaq dc7900, with an ATI Radeon HD 2400 XT (RV610). I am currently running Ubuntu Linux 10.04 LTS with the fglrx driver.

Testing your existing setup

The old way to check for hardware acceleration was the following:

$ glxinfo | grep rendering
 direct rendering: Yes

where a Yes means you do have rendering. However, I learned that a system can answer yes even if it is not using hardware acceleration. The proper command is:

$ glxinfo | grep OpenGL
 OpenGL vendor string: Tungsten Graphics, Inc
 OpenGL renderer string: Mesa DRI Intel(R) 915GM
GEM 20100330 DEVELOPMENT x86/MMX/SSE2
 OpenGL version string: 1.4 Mesa 7.10.2
 OpenGL extensions:

The item of interest is the “renderer string.” If it says “Software rasterizer,” then your system is emulating OpenGL instead of using hardware acceleration. Here is some more documentation on how to check your setup using glxinfo.

Some definitions

  • OpenGL is a standard specification for writing applications that produce 2D and 3D computer graphics. Basically, it is an API.
  • Mesa 3D is an open source implementation of OpenGL, providing the library that applications can call into.
  • Direct rendering interface (DRI) are drivers that Mesa uses to translate OpenGL function calls into GPU-instructions.

When the DRI is present, this would constitute hardware acceleration. It has a userspace component and a kernel space component, which is the direct rendering manager (DRM)

The “driver” that is specified in xorg.conf is actually a relatively basic driver that performs the 2D tasks, including compositing and video acceleration. All 3D calls are passed on to Mesa. See the section about DDX (Display Driver for X) in Linux Graphics Driver Stack Explained.

Kernel Mode Setting (KMS) is the notion that the code to set the video card’s mode is moved into the kernel. The mode is the color depth and resolution of a monitor. Previously, the mode setting code resided in the X server. In the new scheme, it resides in the kernel. This provides the following advantages, as given in Debian 6 Release Notes:

  • More reliable suspend and resume
  • Ability to use graphics devices without X
  • Faster VT switch
  • Native mode text console

Free software on my computer

I am intrigued by Richard Stallman’s free software philosophy, and while I would not consider myself a free software fanatic, I wanted to see how much non-free software I have on my primary computer.

My primary computer is an HP Compaq dc7900. I’m running Ubuntu 10.04 LTS as my main operating system. Right now I have the following non-free software programs on it, and I think this is a representative list for many GNU/Linux enthusiasts:

  • Graphics driver
  • A few games (Sauerbraten, Urban Terror, Assault Cube)
  • Skype
  • Flash plugin
  • Microsoft fonts

These packages can be easily found by using “Virtual RMS” (ubuntu package vrms), which basically gives advice that Richard M Stallman would give if he looked at the list of software on my computer.

Web browser extensions

Then I thought I should examine the list of web browser extensions I use regularly to see if they are open source.

Chrome

Lately Chrome has been my main browser. I like Chrome’s default feature set, and I’ve only needed to add 3 extensions to it.

  • RSS Subscription Extension (by Google). I guess this really is by Google, because all the bug links on the extension info page link to crbug.com, which redirects to the chromium google code project. This doesn’t make clear which license is used.
  • Super Full Feeds for Google Reader. This extension is simple. As soon as you visit the developer website, you can see that the code license is GNU GPL v3
  • Yet Another Google Bookmarks Extension. This developer page does not make clear what license is used.

A Linux OS for my Eee PC

Two years ago I purchased an Eee PC 900 with a 16GB SSD and Windows XP. I left Windows XP on it and installed Eeebuntu 3.0. At this point I don’t remember if I installed Base or Standard. The Eeebuntu people became the Aurora OS people, and they have not released a stable OS since Eeebuntu 3.0.

Eeebuntu 3.0 is based on Ubuntu Jaunty 9.04, and support for Jaunty recently ended, so I will not be able to get security updates for it. To top it off, Google Chrome realized this, and has been complaining to me that my operating system is obsolete.

Windows XP itself is a bit old, and I don’t use it anyway. I decided to wipe out the entire hard drive and install a new operating system. I picked Xubuntu to try out first. I downloaded the Xubuntu ISO and used Ubuntu’s Startup Disk Creator tool to put it on a usb stick.

Backing up

I booted Xubuntu and used the command line to backup using an external hard drive with enough free space for a 16GB image of the entire Eee PC disk.

First, identify the name of the hard drive:

sudo fdisk -l

Then I ran the following command to backup the hard drive:

time sudo dd if=/dev/sda of=/media/ubuntu-backup/home/bobby/eeepcharddisk-20110620.dd
31522176+0 records in
31522176+0 records out
16139354112 bytes (16 GB) copied, 642.54 s, 25.1 MB/s

real    10m43.059s
user    0m40.083s
sys     5m36.741s

Installing Xubuntu

I used the shortcut on the desktop to start the installation.