Saturday, 5 October 2013

Understanding Unix - Part 2 (for programmers)

This post is a sequel of previous post Linux Hacks Part 1 mostly important for programmers.


hack 1 => copy text to the clip(board)
Copy text from file Service.groovy,
$ xclip -selection clipboard grails-app/domain/eccount/Service.groovy

Paste to a new file ServiceNew.groovy
$ vi grails-app/domain/eccount/ServiceNew.groovy

Go INSERT mode and Ctrl+Shift+V

OR

gg"*yG

Reference  :
xclip Does Copy-and-Paste on the Linux Command Line, http://www.linuxplanet.com/linuxplanet/tips/6788/1
VI editor: Copy all the lines to clipboard,  http://stackoverflow.com/a/1620029/432903

hack 2 => find files with a name inside a directory
find searches in the real system.
is slower but always up-to-date and has more options (size, modification time,...)

eg. 1
loohcs$ find . -name "*Service*"
./test/unit/eccount/StudentServiceTests.groovy
./test/unit/eccount/UserServiceTests.groovy
./grails-app/domain/eccount/Service.groovy
./grails-app/domain/eccount/ServiceTransaction.groovy
./grails-app/services/eccount/TransactionService.groovy
./grails-app/services/eccount/UserService.groovy

eg. 2
$ find /packup/repo.softwares/ -name "*cassandra*"
/packup/repo.softwares/JVM/SolrLuceneES-BigData/apache-cassandra-2.0.6-bin.tar.gz
/packup/repo.softwares/JVM/SolrLuceneES-BigData/apache-cassandra-2.0.7-bin.tar.gz

Reference 10:
10 find command in unix examples basic, http://javarevisited.blogspot.com/2011/03/10-find-command-in-unix-examples-basic.html

What is the different between 'locate' and 'find' in Linux?, http://superuser.com/a/199473

hack 3 => awk (intrepreted prog lang)
find the total number of lines in a file (without using NR)
prayag@prayag:~/hacker_/loohcs$ awk 'BEGIN {sum=0} {sum=sum+1} END {print sum}' grails-app/controllers/LoginController.groovy 
134

hack 4 => SED (Stream EDitor
4.1) replace the word "ServiceTransaction" with "Transaction" in file


Stream editor is Stream editor not file editor.

root@0cad383c1249:/# sed --version
sed (GNU sed) 4.4
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Jay Fenlason, Tom Lord, Ken Pizzini,
and Paolo Bonzini.
GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software: <http://www.gnu.org/gethelp/>.
E-mail bug reports to: <bug-sed@gnu.org>.

prayag@prayag:~/hacker_/loohcs$ sed s/ServiceTransaction/Transaction/ < TransactionService.groovy

## OR 
$ echo "Mbr Hlth Screening Mammogram" | sed s/Hlth/Health/ | sed s/Mbr/Member/
Member Health Screening Mammogram


## OR replace a word with special character containing $
sed --in-place 's/shaharma/\$ORGANISATION_NAME/' deployment.sh

4.2) delete empty lines
$ sed '/^$/d' src/main/resources/MoneyReply.xml > src/main/resources/MoneyReplyOut.xml

Practice more at Sed Interview Questions

4.3) remove certain chars

## Following is a script to remove character ) 
$ sed 's/[),]//g' Diagnosis.html

## remove \ character in serialised json,
sed 's/[\\]//g' < data

4.4) replace a line with certain content
Syntax -
sed -i '/TEXT_OF_A_LINE_TO_BE_REPLACED/c\prayagupd just changed this line.' FILE_NAME

eg.
sed --in-place '/Dvbox/c\   "-Dvbox.home=/usr/lib/virtualbox"' project.clj

on macos,

sed -i.bu 's/Dvbox/yo/' project.clj

grep using sed
sed -n '/Chrome/p' < apple.log | tee newApple.log

hack 5 => ssh connect
$ ssh -v uatestinguser@10.13.222.123

Copy file from remote linux server to local machine
$ scp -v uatestinguser@10.13.222.250:/tmp/analytics.csv /home/prayag/

Reference reading : 
Section 1.2: How do SSH, Telnet and Rlogin differ?,  http://cc-ipcp.icp.ac.ru/Section1.2.html
The SSH (Secure Shell) vs RemoteLogin Protocol, http://www.slideshare.net/sourav894/ssh-and-rlogin
3.3 Setting up permissions for rsh, rlogin and rcp(.rhosts), http://rmni.iqfr.csic.es/guide/man/lynux/chap3-3.htm

hack 6 > ls 
list files sorted by date in desc order
$ ls -tr

hack 7 => length of a string

(length of Long.MAX_VALUE in java)

echo "9223372036854775807" | wc -c

      20

$ expr length '$2a$10$osjPAbnn9ogMsY7z.Q7rWe2FLTx/C9Yi6.mj74aTbXWC3iyR1p/0q'
60

hack 8 => find a file with particular text 
The grep syntax looks as below.
$ grep -r "<SEARCH_TEXT>" <DIRECTORY_PATH>

$ grep -r "navbar-merchant" nioc2egdelonk/
nioc2egdelonk/web-app/less/nav.less:.navbar-admin {

hack 9 : know gtk version
$ dpkg -l libgtk[0-9]* | grep ^i
ii  libgtk2-perl                           2:1.223-1build3                         Perl interface to the 2.x series of the Gimp Toolkit library
ii  libgtk2.0-0                            2.24.10-0ubuntu6                        GTK+ graphical user interface library
ii  libgtk2.0-bin                          2.24.10-0ubuntu6                        programs for the GTK+ graphical user interface library
ii  libgtk2.0-common                       2.24.10-0ubuntu6                        common files for the GTK+ graphical user interface library

hack 10 : install gtk-recordmydesktop
$ sudo apt-get install gtk-recordmydesktop

Configure laptop settings as mentioned at recordMyDesktop output video shows *massive* artifacts, else plays too fast lowering resolution to to 1360x768.
xrandr -s 1360x768



Start recordmydesktop 
$ gtk-recordmydesktop -o /home/prayag/Videos/gwt_screencast.ogv

Convert *.ogv to *.flv with same-quality (size will increase though)
$ ffmpeg -sameq -i /home/prayag/Videos/gwt_screencast.ogv /home/prayag/Videos/gwt_screencast.flv

hack 11 : Zip a file
$  zip -r eccount-hg *



References
How can I create launchers on my desktop?, http://askubuntu.com/a/64237/37643
Csv to Html Table, https://gist.github.com/prayagupd/8050493

No comments:

Post a Comment