Print/Count Unique Words in a File Using Command Line

Here is a one-line-command used to print/count unique words in a file:
# Print
cat file_name | tr " " "\n"| sort | uniq
# Count
cat file_name | tr " " "\n" | sort | uniq -c
# tr " " "\n" used here to replace space with newline, what it does is to put every word separated by space into a line

sed and awk

Use sed and awk you could print certain lines that meet your query condition.
Here is a command I used to print book titles of my son's reading history from a text file. From the picture, we can tell that book titles are mixed with DVD titles and the author names are listed after titles if there is any.


My task is to pick up titles of books only. Here sed and awk comes handy.
sed -n '/TITLE/p' history.txt | sed -n '/DVD/!p' | awk '{FS="/"; print $1 }' | awk '{FS="        "; print $2}'
By default sed will print anything from the original file.
-n '/TITLE/p' will let sed print only lines contain 'TITLE'.
-n '/DVD/!p' will further suppress lines contains 'DVD'
awk '{FS="/"; print $1 } will remove the author information from title
Further investigate reveal that there are 8 spaces between TITLE and the title of the book,
awk '{FS=" "; print $2}' will print out only the title of the book.

Autostart application after CentOS reboots

If you only want to run a script after CentOS boots, you could put the command to rc.local file.
vim /etc/rc.d/rc.local
# type in your script at the bottom of this file
If you want to start and shutdown the program along with the system, you might need to create a service using chkconfig. To do so,

1.create a script file at /etc/init.d/
Here is an example of the content of the file
# create file( you need root privilege to do)
cd /etc/init.d/
vim emen2
and add the followings to the file
#!/bin/sh
#chkconfig: 235 80 05
#description: EMEN2 database server
#source function library
EMEN2_HOME=/srv/EMAN2
if [ ! -f $EMEN2_HOME/eman2.bashrc ]
then
  echo "EMEN2 startup: cannot start, missing eman2.bashrc"
  exit
fi
case "$1" in
  "start")
    echo -n "Start emen2 service"
    source $EMEN2_HOME/eman2.bashrc
    # To run it as root
    $EMEN2_HOME/Python/bin/emen2ctl start -h /srv/db_env -e default,site,em --port 8080
    # Or to run it as dedicated user
    /bin/su - username -c $EMEN2_HOME/Python/bin/emen2start
    echo "."
    ;;
  "stop")
    echo - n "Stop emen2 service"
    source $EMEN2_HOME/eman2.bashrc
    # To run it as root
    $EMEN2_HOME/Python/bin/emen2ctl stop -h /srv/db_env -e default,site,em --port 8080
    # Or to run it as dedicated user
    /bin/su - username -c $EMEN2_HOME/Python/bin/emen2stop
    echo "."
    ;;
  *)
    echo "Usage: /sbin/service emen2 {start|stop}"
    exit 1
esac
exit 0

**About the three values after #chkconfig
(1). The first value ’235′ states the default runlevels at which the service should be started. In this case, the service is configured to start at runlevel 2,3 & 5.
(2). The second value ’80′ states the start priority of the service. In this case the service shall be started after starting all those services having start priority less than 80.
(3). The third value ’05′ states the stop priority of the service. In this case the service shall be stopped after stopping all those services having stop priority less than 05.

**/bin/su -c can only accept simple command, which means there should be no "-" or "--" in the command.

2. make the file runnable:
chmod a+x emen2
 
3. add the newly created service to the start up list by
/sbin/chkconfig/emen2 on

Use wget to detect broken links in a website

while I was updating a website developed using Django, found out that some links were broken due to the change of Django version. To locate all broken links, I used the following commands:
wget --spider -r -l 1 --header='User-Agent: Mozilla/19' -o broken_links.txt http://example.com/updates/
Then I used grep to see whether 404 error was appearing in the output file.
grep -B 2 '404' broken_links.txt
# -B 2 :Print 2 lines of leading context before matching lines which contains the broken link

Install perl DBD::Oracle module without installing Oracle server onCentOS

I am in the situation of using ora2pg to migrate an Oracle database to postgreSQL. To install ora2pg, DBD::Oracle is required. To install DBD:Oracle, the environmental variable $ORACLE_HOME is required. However, it is not easy to set up an environmental variable for $ORACLE_HOME since I am using a remote dedicated Oracle server. To overcome the problem, I downloaded and installed the following three rpm packages: Please be advised that it should be no problem to set $ORACLE_HOME on a machine which happens to be the Oracle server.
# download link for Linux 64bit
http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html
oracle-instantclient11.2-basic-11.2.0.3.0-1
oracle-instantclient11.2-devel-11.2.0.3.0-1
oracle-instantclient11.2-sqlplus-11.2.0.3.0-1
I then added the $ORACLE_HOME to ~/.bashrc file.
#By default, the installation path is /usr/lib/oracle/11.2/client64/
export ORACLE_HOME="/usr/lib/oracle/11.2/client64/"
If you want connect to the Oracle database from command line using sqlplus, you could add $ORACLE_HOME/bin to you PATH, though the syntax for connecting to a remote database using its SID is nasty.
PATH=$PATH:/usr/lib/oracle/11.2/client64/bin
sqlplus AERS/AERS@'(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=remote_server_address)(PORT=1521)))(CONNECT_DATA=(SID=sid_code)))'
Now, you have done all the steps for running sqlplus on a command line. Will you enjoy the using? You probably not at this moment. Since running sqlplus without further tweaking is a pain in Linux command line: you do not have your history(using upper arrow) or you can not use left/right arrow to edit your sql command. Here is a solution, using rlwrap
# install required repository
wget http://www.mirrorservice.org/sites/dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
sudo rpm -Uvh epel-release-6-8.noarch.rpm
# install rlwrap if it does not exit in your system
sudo yum install rlwrap
#then run
rlwrap sqlplus ***
You could also add he following alias in your ~/.bashrc file
vim ~/.bashrc
#add the following into the file
alias sqlplus='rlwrap sqlplus'

Install SQL Developer on CentOS 5

1. Configure $JAVA_HOME if there is nothing outputs when you run
echo $JAVA_HOME
Mine is at /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/ so I put the following line into ~/.bashrc file:
export JAVA_HOME="/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/"
2. Download SQL Developer installation from Oracle SQL Developer RPM for Linux and install.
sudo rpm -ihv sqldeveloper-3.2.20.09.87-1.noarch.rpm
3. add /opt/sqldeveloper to your PATH
PATH=$PATH:/opt/sqldeveloper/
4. Run SQL Developer:
source ~/.bashrc
sqldeveloper

Show Time Stamp on Bash History

Ever wondered when did I execute the command? You could add a time stamp in front of each command you executed.
echo 'export HISTTIMEFORMAT=" %F %T "' >> ~/.bashrc
source ~/.bashrc
You will see time stamps on your commands when you execute history next time.
The result will look like:
1013 2013-05-09 14:47:44 vim scripts_for_Distance_Matrix.txt
1014 2013-05-09 14:48:01 R < scripts_for_Distance_Matrix.txt --no-save

Datatable static image not found on the server

When you use ```datatables.min.css``` and ```datatables.min.js``` locally, instead of datatables CDN, you may have encountered that ```sort...