Thursday, January 1, 2015

Happy new year 2015 to all unix lovers

Wishing all the readers of a Happy and Healthy 2015 ahead, keep rocking !

Saturday, December 20, 2014

Unix - Delete blank lines from file

Here's some ways to remove empty or blank lines from a file in Unix. Simple but definitely useful.
$ grep -v '^$' file.txt
$ grep '.' file.txt
$ sed '/^$/d' file.txt
$ sed -n '/^$/!p' file.txt
$ awk NF file.txt
$ awk '/./' file.txt

In Vi editor, in escape mode type
:g/^$/ d
Related posts:

Saturday, December 13, 2014

Execute script on remote server SSH - Unix

Assume you have a complex bash script named '' (containing good number of loops, Awk statements, multiple lines of code etc) that you need to run against one or more remote servers via SSH. One way would be to copy (i.e. scp) the script to the remote server(s) and then do something like this: e.g.
$ scp -2 root@

$ ssh -2 root@ sh /tmp/
If its multiple machines, then you can use bash for loop like this:
$ for ip in ; do echo "Executing on IP: $ip" ; ssh -2 root@$ip 'sh /tmp/' ; done
Now assume you do not want to copy your script to the remote machine(s) and want to execute from your local machine without copying it. Here's a way:
$ cat | ssh -2 root@ /bin/sh
And if its to be executed on multiple machines, you can use a for loop like this:
$ for ip in ; do echo "Executing on IP: $ip" ; cat | ssh -2 root@$ip /bin/sh ; done
I am sure you liked this trick ! Feel free to comment below. Thanks.

Related posts:

© Jadu Saikia