Using command-line Perl to search and replace

One of things I always seem to be doing is searching for strings in files and changing them.   I’ve gotten used to using Sed and VI for this.  But perl also makes it easy to do via the command line.

Consider the following text file:

$ cat resolv.conf
search theunixzone.com
nameserver 10.1.1.1
nameserver 10.1.1.2
nameserver 10.1.1.3

By doing a command similar to the following, you can easily change strings from the command line.

$ perl -pi -w -e ’s/10.1.1/10.5.5/g;’ resolv.conf

$ cat resolv.conf
search theunixzone.com
nameserver 10.5.5.1
nameserver 10.5.5.2
nameserver 10.5.5.3

Often, the benefit of using Perl to do this is the fact that you can specify multiple filesnames, or use a wildcard, and edit the files in-place — without writing to a temporary file.   I use this often for making mass changes to DNS Zone files, etc.

Let’s look at the command line argruments we used.   The “-pi” allows Perl to edit the file in place.  The “-w” enables many useful warnings.   And, finally, the “-e” basically says that the next string in quotes is the perl program instruction you want to exectute.  In this case, we are changing all occurances of the string “10.1.1″ and changing it to “10.5.5″.    This is a regular expression which we’ll cover more of in the future.

350,000+ Virtual Desktop Instalation in Brazil

eWeek writes that Brazil now has the largest single desktop virtualization and Linux deployment.  Over 350,000 workstations in schools.

Personally,  I think getting only 10 virtual workstations to a single server is pretty week.  Memory is cheap, and I doubt the CPU requirements are going to be all that demanding.  I’d probably pushing to get at least 20 to 25 per server.  To me, that would seem to “really” be worth it.  Although 10 to 1 does provides a significant savings both in monetary resources, as well as being green for our environment, to make it worth it.

Virtualization is very cool.

HP Certification for Ubuntu

According to Canonical, plans are in the works to get Ubuntu fully certified on HP hardware.

I’ve always been a fan of Hewlett-Packard and have used their Intel, PA-RISC, Alpha, and Itanium servers forever running UNIX and Linux.  On the Intel side, this has been primarily SUSE and RedHat Enterprise Linux due to official vendor support.

Working towards hardware certification with a vendor such as HP, will be a great step for Ubuntu.  Often, this is the main reason why one Linux distribution is chosen over another.

This is great news!

COTD: wc - print newline, word, and byte counts for each file

The wc command is very useful.  It’s a simple little command, but I use it all the time.  The wc command basically counts the number of characters, number lines, or the number of words.  Some version can also show the length of the longest line.

Using it is simple.  Pass the output of another command to it, or, give it the name of a file to read.  For example:

$ cat /etc/services | wc -l
562
$

Here we use the cat command on the /etc/services file, and then piped the output (”|”) to the wc command with the -l option (for lines).   The output?  562 lines.

Now, lets do the same thing only with characters.

$ cat /etc/services | wc -c
18449
$

Here we see that the /etc/services command has 18,449 characters in it.

Finally, we will do the same for words.  Maybe a better example would be for a book report, or paper that you need to write that needs to be at least 2000 words:

$ cat ~/homework/book_report_on_unix.txt | wc -w
2544
$

Plenty of words.  The uses for wc are endless, and I will point out it’s use in examples that I show here moving forward.

Questions? Feel free to ask.