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 !
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 :
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 !
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:
If you want to see how to script a text replacement, check out my previous post about text replacement with sed.
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
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.
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.