Wednesday, December 29, 2010

Howto display whole file without first line - AWK

I was asked lately to write a one liner in bash/sh/csh to display the whole file content without the first line, a bit weird and perhaps unnecessary task but still some what challenging.

For example if this the the content of file.txt:

The data wanted is:

After a bit of head scratching I came up with this not elegant solution:

# length=`cat file.txt|wc -l`;cat file.txt| tail -`${length} -1`

But the output was a bit clumpsy and it's more of a 2 commands chain and not exactly a one liner.. So, after searching for a solution  I came up to this gem:

#cat file1.txt|awk FNR-1

Another Option:

You can use the following syntax to make awk print from one string to another, thus ommiting the first line (just change beginning & end strings):
 # awk '/Beginning/,/End/'

When used correctly awk can be your swiss army knife.

Saturday, December 25, 2010

Howto force fsck or bypass fsck in Linux

A nice little trick to force/bypass fsck:

To force fsck, type (as root):
#touch /forcefsck

To bypass fsck (as root):
#touch /fastboot

Boot the machine, and whoala!

Tuesday, December 21, 2010

Unix/Linux - Print out "string" between "match1" and "match2"

I was looking for a way to print text between "match1" and "match2", these two commands will achieve this:
sed -ne 's/.*\(match1.*match2\).*/\1/p'

egrep -o 'match1.*match2'

Wednesday, December 15, 2010

Check network connectivity with Perl

Sometimes it's crucial to check network connectivity to some server before running some script that will run on it, I've been searching for the easiest way to check network connectivity (ping) in Perl (without sprintf or system) and looks like I found one, the script uses the "Net::Ping" module.

The following script takes a server name as argument and verifies wether it's up or down:

#!/usr/bin/perl -w

use strict;

use warnings;

use Net::Ping;
my $master=$ARGV[0];

my $p = Net::Ping->new();

if ($p->ping($server))


printf "$server is up\n";

exit (0);
} else {

print"the server $server has no ping \n";



Monday, December 6, 2010

Howto - Find broken symbolic links

2 cool ways to find broken symbolic links on your system:
for i in `find /`; do if (test -h $i); then file $i|grep broken; fi; done

./bar_link: broken symbolic link to `/etc/foo'

Any Shell:
find / -type l ! -exec test -r {} \; -print


When measuring the results with "time" command - the 2nd command wins, so if performance is critical in your case, use the 2nd one :)