Perl sed style command

From http://www.itworld.com/nl/unix_sys_adm/09252002/

perl -i -p -e s/floor/hamper/ig $file
perl -i -p -e ‘s/hamper = no/hamper = yes/’ $file
perl -i -p -e ‘s/floor\s+yes/floor\tno/’ $file
perl -i.bak -p -e s/floor/hamper/ig $file

Each of these commands edits the file referenced by $file “in place”. That is, it doesn’t create another file. It opens the file read/write and makes the change requested with no changes to the file except the one intended.

The -i argument tells Perl to make the change in-line using the file for both reading and writing. This is routine for Perl. If a -i is followed by a file extension (as shown in the 4th example above), a backup of the file is created before the program is run.

The -p tells perl to assume a loop around the one-line program and to echo the output.

The -e specifies that the one-line program is coming next. In all of our examples, we’ve provided a sed-like change command.

Now let’s look at how Perl substitutions work.

The leftmost expression in a s/this/that/ command can be anything from an exact match to a fuzzy match. We can match literally on the word “this” or we can match on any word that starts with t or any four-letter word if we want to. It all depends on the expression that we use. The rightmost expression is always exact, of course. In an odd sort of way, a Perl substitute command is like me asking my stepkid to pick up the clothing from her floor and put it in her hamper. If I were too exact when I told her what to pick up, she would pick up only that. If I were not precise on what I wanted her to do with the clothes she picked up, they’d probably end up under the bed or on the floor of her closet. Clearly we need to match on clothing anywhere on the floor and specify the exact destination as the hamper.

perl -i -p -e s/floor/hamper/ig $file

In this particular example, we’ve simply changed “floor” to “hamper”.

perl -i -p -e ‘s/hamper = no/hamper = yes/’ $file

In this example, we’ve included more complex strings. No problem.

perl -i -p -e ‘s/floor\s+yes/floor\tno/’ $file

This example is a bit more interesting. We’ve matched on “floor” followed by white space, followed by “yes” and changed it to “floor” followed by a tab followed by “no”.

perl -i.bak -p -e s/floor/hamper/ig $file

If we include an file extension as shown in the line above, we’d be left with a file called $file.bak containing the original text. This is a good idea if you might have unwanted side effects. The “ig” at the end specifies that we’re ignoring case and changing every instance of the word that appears in each line (not just the first). Ignoring case it itself a big time saver in many situations.

For “zapping” files, Perl is hard to beat.

Format hard drive using fdisk

Got an ATA hard drive hooked to an external USB package and need to reformat the disk to ext3 system.

First, I run fdisk to see what content it has
fdisk -l /dev/sda1

Then, I re-partition it using
fdisk /dev/sda1
select option d to delete the old partion,
and then option n to add a new partition, where I choose the primary partition instead of the extended one. There will be complaint about it’s too large, but since this disk is not going to be used as a boot disk, so I don’t care

Next, I create the ext3 file system on the partition
/sbin/mkfs -t ext3 /dev/sda1

After it’s done, I can mount it to my Linux system
mount /dev/sda1 /disk3

Optionally, I can change the percentage of reserved blocks using tune2fs, e.g., from the default 5% to 3%
tune2fs -m3 /dev/sda

Install NTFS driver for CentOS

First, I download the driver by visiting http://www.linux-ntfs.org

Second, there are two versions of rpm files available for Redhat Enterprise 4, kernel 2.6.9-34.ELsmp, which I downloaded both. But none works. I found there installed locations by running rpm again

rpm -qpl kernel-module-ntfs-2.6.9-34.0.2.ELsmp-2.1.20-0.rr.10.0.i686.rpm

It showed the location is /lib/modules/2.6.9-34.0.2.ELsmp/, but the correct kernel library path is /lib/modules/2.6.9-34.ELsmp/. That’s why modprobe failed to find the installed file. So I copied the file to the correct path

cp -R /lib/modules/2.6.9-34.0.2.ELsmp/kernel/fs/ntfs /lib/modules/2.6.9-34.ELsmp/kernel/fs/

Then I successfully run
modprobe ntfs
dmesg | grep ntfs
mount /dev/sda1 /disk3 -t ntfs -r -o umask=0222

The problem is that the disk mounted is read only