Wednesday, February 4, 2009

Vi Search and Replace

Vi Search and Replace

Search and Replace

To begin a search and replace in Vi or Vim, start by hitting the colon <:>. This will allow you to enter a Vi command. Then, enter a search and replace command like so:

%s/old/new/g

This will search every line for 'old' and replace all occurrences in the line with 'new'. You start by mentioning which lines to affect. In my example, I use '%s'. This translates to all lines. Then, I trail the command with 'g' (for global) to affect all occurrences in the lines defined by '%s'. Note that global does not mean the entire file!

Limiting the Replace

You can declare the search and replace for only the current line by removing the percent sign:

s/old/new/g

It is also possible to limit the affected line numbers. The next example only affects lines 40 through 42:

40,42 s/old/new/g

Eliminating the 'g' from the end will only replace the first occurrence per line defined in the search. For example, the following replaces the first instance of 'hello' on every line in the file:

Regular Expressions

The search string can be the form of a regular expression. In the next example, I replace all numbers with the word 'number'.

%s/[0-9]/number/g

Escaping Characters

You will have to escape out the slash if it is part of your search string:

%s/http:\/\//https:\/\//g

Use Any Delimiter

Alternatively, change your delimiter. It can be anything!

%s!http://!https://!g

Get Confirmation Before Changing

You can get confirmation for each replace by adding a 'c' to the end of your command:

For more information, see the online Vi documentation.

Flash Plugin on Fedora 10


The Adobe Flash Plugin, with sound support, is available on Fedora. These instructions guide you through the installation of Adobe's Flash Plugin on Fedora 10.

Install Audio Libraries

First, install the audio libraries needed for sound support by the Flash Plugin.

su -c "yum -y install pulseaudio-libs alsa-lib alsa-plugins-pulseaudio"

Using 64-bit Firefox?

Adobe Labs has been offering a 64-bit Flash plugin since Dec 12, 2008. Be sure to first uninstall any other versions of Flash you have installed on the system. Then, download and unpack the 64-bit Flash Plugin for Linux into your Mozilla Plugins folder. For updates and details, visit the Adobe Labs Download page for Flash: http://labs.adobe.com/downloads/flashplayer10.html

cd $HOME/.mozilla/plugins
wget http://download.macromedia.com/pub/labs/flashplayer10/libflashplayer-10....
tar -xzvf libflashplayer-10.0.d21.1.linux-x86_64.so.tar.gz

Don't forget to restart your browser!

32-bit Users Only

Visit http://www.adobe.com/products/flashplayer/ and click Download Now. Then select YUM for Linux and download the provided RPM (adobe-release-i386-1.0-1.noarch.rpm).

Install the downloaded RPM which installs /etc/yum.repos.d/adobe-linux-i386.repo.

su -c "rpm -ivh adobe-release-i386-1.0-1.noarch.rpm"

Check that you can access the Adobe Yum repository.

su -c "yum --disablerepo=* --enablerepo=adobe* list"

Now, install the Adobe Flash Plugin

su -c "yum install flash-plugin"

Then, add the Flash Plugin is in your $HOME/.mozilla/plugins folder.

mkdir -p $HOME/.mozilla/plugins
cd $HOME/.mozilla/plugins
ln -s /usr/lib/flash-plugin/libflashplayer.so .

Don't forget to restart your browser!

Compile PHP on Fedora

Below is an outline on how to compile a feature rich, PHP Apache module on Fedora 9. First, compile Apache on the system using these instructions. This document assumes that Apache and the APache eXtenSion Tool are installed in the PREFIX: /opt/apache2.

Once you have Apache prepared, download the PHP source from php.net. As of August, 2008 the latest version is PHP 5.2.6. So, the following examples will use the php-5.2.6.tar.gz source tarball. Use your browser to download the latest and greatest. Then, untar the source.

tar -xzvf php-5.2.6.tar.gz
cd php-5.2.6

Next, configure the source for compile. Use the configure command. You can provide --help to see the large list of options.

./configure --help

For every option you enable, make sure you have the necessary RPMs installed to build PHP. For example, if you run configure with --with-snmp, you will need the net-snmp-devel RPM installed to complete the build. To ensure that you can build PHP with a full suite of options, install the following RPMs. Trim the list to suit your needs.

yum install aspell-devel curl-devel cyrus-sasl-devel e2fsprogs-devel freetype-devel glibc-devel keyutils-libs-devel krb5-devel libgcc libidn-devel libjpeg-devel libpng-devel libselinux-devel libsepol-devel libstdc++-devel libX11-devel libXau-devel libXdmcp-devel libxml2-devel libXpm-devel mysql-devel net-snmp-devel openldap-devel openssl-devel tcp_wrappers zlib-devel

Then, configure with the following settings. Change the configure --prefix to adjust your install PREFIX. Again, trim the list to suit your needs.

./configure --prefix=/opt/php5 --with-apxs2=/opt/apache2/apxs --with-mysql=/usr/bin/mysql_config --with-mysqli=/usr/bin/mysql_config --with-png-dir=/usr --with-gd --enable-gd-native-ttf --with-ttf --enable-safe-mode --enable-magic-quotes --with-pspell --with-gettext --with-jpeg-dir=/usr --with-zlib --with-curl --enable-soap --with-ldap=/usr --enable-sockets --with-openssl --with-snmp --enable-mbstring --with-freetype-dir=/usr --with-xpm-dir=/usr --with-libdir=lib64

Please note that if you are on a 32bit system, remove:

--with-libdir=lib64

Finally, build PHP and test it.

make
make test

If you are comfortable with the results (a few errors are OK in make test), then install PHP.

sudo make install

The install will place the PHP module in /opt/apache2/modules and prepare /opt/apache2/conf/httpd.conf with these settings:

LoadModule php5_module modules/libphp5.so

Then, place a php.ini in /opt/php5/lib (or your PREFIX/lib).

cp -a php.init-recommended /opt/php5/lib/php.ini

Finally, prepare your Apache Virtual Host to support index.php with the following code:

AddType application/x-httpd-php .php .inc .class
AddType application/x-httpd-php-source .phps
DirectoryIndex index.html index.php

The SysAdmin Ten Commandments


I. Thou shalt respect thy user

It is paramount that you respect the user and their data. Don't steal their MP3s, don't look at their porn. When working with email, look without reading. Respect them and they will respect you.

II. Thou shalt empower the user

The user is the customer. The system is not yours, it is theirs. Provide users the tools they need to be successful and they will be happy. Give them space to experiment, learn and create. Do not hold users back.

III. Thou shalt keep it simple

Keep It Simple, Stupid. The KISS principle will keep you from building barrels to keep a bottle's worth of wine. Do not mistake over-engineering for quality. Quality work is concise and scalable.

IV. Thou shalt expect catastrophe

Expect the worst surprises. Make your resources redundant, backup your data, test your backups, and then backup the backups. The shit will hit the fan someday. Be prepared.

V. Thou shalt plan

Great work follows great plans. Stay on target by first identifying it. Plan your work carefully and surprises will be easier to handle.

VI. Thou shalt stay informed

Gather all the news, information, logs and statistics you can. Without the raw data to analyze, you cannot be secure, you cannot debug problems and you cannot forecast. Without new information, you cannot learn.

VII. Thou shalt share

No one is alone in this world. Pass your knowledge on to others. Give to others, contribute to the community and you will reap many rewards. It is not wise to horde knowledge. Don't be an asshole.

VIII. Thou shalt automate

Do more, faster by automating tasks. You are the puppet master, do not waste your time with menial work. Work smarter, not harder.

IX. Thou shalt document

Always document your work, your code and your plans. Then document their execution and maintenance. Document for others and document for yourself. Documentation should be an extension of your mind and a resource for others.

X. Thou shalt respect thy organization

Without organization, there is no system, there is no user and there is no job. Understand the organization and it's business. Respect it and help it thrive.

Multimedia support in fedora

yum -y install libdvdcss libdvdread libdvdplay libdvdnav lsdvd libdvbpsi totem-xine xine-lib-extras-nonfree xine-lib-ex
tras gstreamer gstreamer-plugins-good gstreamer-plugins-bad gstreamer-plugins-ugly compat-libstdc++-33