Regular expression, find and replace using Unix tools


I need to add some text to a lot of text files. Previously, I used jEdit to perform regular expression for several files. But currently, I need to add the text for about 120 files. In order to do this, I tried using Unix tools, “sed”, “grep”, and “xargs”.

Firstly, I tried MSYS, yet it fails. Because the “sed” version was too old. Then, I used Cygwin.

My problem was to add the following text to the text files:

Background { skyColor 1 1 1 }

Since the text files are computer generated, all the text files have following text:

DEF Camera

And the text only occurred once for each file.

Therefore, the best solution is to use regular expression for search and replace to produce the following result:

Background { skyColor 1 1 1 }

DEF Camera

Therefore, I used the following command to solve the problem. (Do not copy paste, because the user must type the “newline”).

$ grep -l "DEF Camera" *.wrl | xargs -l sed -i -e 's/\(DEF Camera\)/Background { skyColor 1 1 1 } \
> \
> \1/g'

First of all, all the files are in .wrl format. So, I used

grep -l "DEF Camera" *.wrl

to get all the file names where the files contain “DEF Camera”. The option “-l” indicates the result is the file name. Then I used xargs and sed to search and replace the text (actually I am not familiar with “xargs” command). Then,

sed -i -e 's/\(DEF Camera\)/Background { skyColor 1 1 1 } \
\
\1/g'

“sed” command cannot write newline like C, such as ‘\n’. Thus, I need to type ‘\’ followed by ‘enter’ key. This will produce exact newline to the I/O stream. Unlike jEdit or PHP regular expression, one needs to use ‘\(‘ and ‘\)’ instead of ‘(‘ and ‘)’ for replacement by reference. And the ‘\1’ is used instead of ‘$1’ for the reference.

Finally, using this command helps me to add the text to about 120 files with several seconds. Good!

Advertisements

Try out Ubuntu, Wubi, and Portable Ubuntu Remix


Ubuntu, in my opinion, is a great Linux distribution. There are several ways to let you try out Ubuntu. After download the CD, I can burn it to the CD and boot from the CD. I can try Ubuntu as a LiveCD. I can also boot it from a USB pendrive like a CD.

When installation, I can create a new partition on my harddisk. I can install it on the logical partition and boot from there. Unlike FreeBSD, which needs to be installed on a primary partition. Not only that, I can also install Ubuntu side by side with the same partition with other OS (such as Windows). This is special.

Besides, the CD image download from Ubuntu website contains also Wubi. Which I can install the Ubuntu from Windows (however I failed to install on Windows 7). Installing with Wubi will create a file and the file will be assumed as an OS itself. Then, one can choose to boot into Ubuntu installed by Wubi when the computer started.

But if one does not want several OSes on a computer, he/she can try Portable Ubuntu Remix. You can run Ubuntu on your Windows. But the TRES version requires about 4GB after extraction. (I tried to use “sed” command through Portable Ubuntu to do some operations on my computer, it fails.)

It is worth to try out.

Installing CVS on Ubuntu 10.04


I found the following message when I was trying to install CVS on Ubuntu 10.04 with synaptic,

Please insert the disk labeled: Ubuntu 10.04 LTS _Lucid Lynx_ – Release i386 (20100427.1) in drive /cdrom/

I am not sure why is this message appears. But my Ubuntu 10.04 was upgraded from Ubuntu 9.10 with the alternative CD. Fortunately, I got the CD image. Thus, to solve this problem, I mount the alternative CD to the “/cdrom/” drive. Then, there is no problem for the installation.

sudo mount -o loop ubuntu-10.04-alternative-i386.iso /cdrom/

Only the alternative CD works, the desktop CD cannot work.

Huawei Mobile Broadband, Ubuntu 10.04, Celcom service, and HP Mini netbook


Huawei Mobile Broadband, which I bought about 2 years ago, is failed to run properly on my HP Mini 1000 series with Ubuntu 10.04. It will keep trying to connect to internet, yet failed. And I also found a “MOBILE_CONNECT” icon on the desktop. If I tried to click it, it will immediately missing.

So, to solve this problem,

  1. I right-click the NetworkManager Applet,
  2. Edit Connections…
  3. Goto Mobile Broadband tab
  4. Click Add button
  5. Then it will show the Huawei broadband device
  6. Continue the steps for my service provider, which is Celcom
  7. Can connect to internet properly.

I do this whenever I cannot connect to internet properly using the Huawei Mobile Broadband.