Licence
Creative Commons License

This work by Stephane KATTOOR is licensed under a Creative Commons Attribution 3.0 Unported License.
Feeling like tipping ?
If you find this blog useful, you might consider sending a few bitcoins to support it : 1BTtsC3beGJ6ysd8DhrXjdo6jVw5WD9mvY
RSS
 
RSS Feed
Follow me !
Tech@Sakana on Facebook
Search this site

Newsletter

Get latest posts by email (No spam, only posts):

Enter your email address:

Delivered by FeedBurner

Categories
Monthly archives
October 2007
M T W T F S S
« Sep   Nov »
1234567
891011121314
15161718192021
22232425262728
293031  
Month: October 2007
awk : one-liner to select a field - October 27, 2007 by Stephane Kattoor

Getting a specific field of a line with awk is really simple. For example :
spaghetti% echo "test1 test2 test3" | awk '{print $2}'
test2

A more “real life” example is as following, which will find in /etc/hosts the IP address of localhost (pick another host if you wish :) ) :
spaghetti% cat /etc/hosts | awk '/localhost/ { print $1;}'
127.0.0.1
::1

If the field separator is not a space, awk will let you change it with the FS variable :
spaghetti% echo "test1:test2:test3" | awk 'BEGIN { FS = ":"} { print $2 }'
test2

This should be enough to get you started !

Note : As suggested by Miljan, there’s a much simpler way to change the Field Separator : the -F option switch. So the example I provided can be rewritten as follows :
spaghetti% echo "test1:test2:test3" | awk -F: '{ print $2 }'
test2

Cool. Thanks Miljan !

Apache : Perform load tests and bench Apache - October 23, 2007 by Stephane Kattoor

If you need to simulate a load on an Apache server (or any web server actually), you can use Apache Bench, which is included in the standard Apache HTTPd distribution. This tool will launch connections to your webserver as instructed to simulate multiple users and will help you to tune your Apache settings.

You can find the synopsis at the Apache website. Most common options are :

  • -n : number of requests to perform
  • -c : number of concurrent requests

Other options allow you to control precisely the request to send, proxy settings, user authentication, cookies and much more.

Question to readers : Which HTTP load tester do YOU use ? TIA for sharing !

vi : replacing text globally (search and replace) - October 20, 2007 by Stephane Kattoor

If you want to replace a text throughout a file in the vi text editor, you can use the following command :
:1,$s/text/replacement/g

Here is the breaking down of this command:

  • :” : places you in “ex mode”
  • 1,$ is a range specification, meaning from the first to the last line (this can be shortened as % in some versions of VI (vim does, for instance) — Thanks Brandon !
  • s means “substitute”
  • /text/replacement/ means to replace the “text” pattern by “replacement”
  • g : means globally, which will replace all the occurrences of the pattern instead of the first of each lines

If you want to see how to script a text replacement, check out my previous post about text replacement with sed.

sed : replacing a text in a file - October 20, 2007 by Stephane Kattoor

To replace a text in a file, you can invoke sed as in the following example :

1
% cat file.txt | sed -e 's/text/replacement/g' > result.txt

This will change all the occurences of “text” to “replacement” in “file.txt” and output the result in “result.txt”

Note : As suggested by Matthias from adminlife in the comments, if you wanted to do “in place” text replacement (that is modify the file without a temporary file in between), you can do the following :

1
sed -i ’s/text/replacement/g’ file.txt

For more complicated text manipulations you might consider moving to Perl, but sometimes you don’t need the sledge-hammer :-)

Linux – RedHat (or CentOS) : update system and packages with yum - October 20, 2007 by Stephane Kattoor

In order to keep your system updates (especially important for security fixes) on a RedHat linux system (or CentOS), you can simply perform the following command :
yum update
You’ll then be presented with a list of available updates for your system.

Doing so from time to time will help you to keep your system secure and to get the latest versions of your softwares.

If you wonder how to do the same with a Debian Linux system, check out my previous post about Apt, the Debian package manager.

Linux – RedHat (or CentOS) : list installed packages - October 20, 2007 by Stephane Kattoor

If you need to list packages which are installed on a RedHat system, you can do so by issuing the following command :
rpm -aq

Alternatively, if you prefer to use the package manager, you can try this command :
yum list installed

if you want to know how to do this on Debian, check out my previous post on Apt.

Who am I ?
Ads