Thursday, January 1, 2015

Happy new year 2015 to all unix lovers


Wishing all the readers of unixcl.com 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 'run_q.sh' (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 run_q.sh root@192.168.32.8:/tmp/.

$ ssh -2 root@192.168.32.8 sh /tmp/run_q.sh
If its multiple machines, then you can use bash for loop like this:
$ for ip in 192.168.32.8 192.168.32.9 192.168.32.3 ; do echo "Executing on IP: $ip" ; ssh -2 root@$ip 'sh /tmp/run_q.sh' ; 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 run_q.sh | ssh -2 root@192.168.32.8 /bin/sh
And if its to be executed on multiple machines, you can use a for loop like this:
$ for ip in 192.168.32.8 192.168.32.9 192.168.32.3 ; do echo "Executing on IP: $ip" ; cat run_q.sh | ssh -2 root@$ip /bin/sh ; done
I am sure you liked this trick ! Feel free to comment below. Thanks.

Related posts:



© Jadu Saikia www.UNIXCL.com