Wednesday, September 16, 2009

Login Activity on a Unix Server

To check the login activity on a Unix server you can run the following command:
# last | head

'head' just displays the last 10 logins.

You can also check the login activity of a specific user:
# last user_name | head

Total Memory Installed on Solaris

Use the prtconf command to get the total amount of memory installed on a Solaris machine:
# prtconf

Tuesday, September 15, 2009

Counting the number of occurrences of each line in a file

To count the number of occurrences of each line in Linux you can use the following command:
sort input_file | uniq -c > output_file

You may want to ignore the case and then apply the count:
cat input_file | tr '[:upper:]' '[:lower:]' | sort | uniq -c > output_file

Monday, September 7, 2009

Removing punctuation from a file in Linux

To remove the punctuation from a file run the following command:
cat input_file | tr -d '[:punct:]' > output_file

Converting all Upper Case letters to Lower Case in Linux

To convert all upper case characters to lower case in Linux, run the following command:
cat input_file | tr '[:upper:]' '[:lower:]' > output_file

Thursday, August 27, 2009

Obtaining a specific field from a comma separated file (using awk)

If you have a comma separated file e.g.
Name,DateOfBirth,PlaceOfBirth
Jeff,17-09-1982,London
Maggie,08-06-1976,New York
George,14-02-1991,Johannesburg


and you need to extract only the list date of birth from this file you can use the following awk command:
awk 'BEGIN{FS=","}{print $2}' filename > date_of_birth

where FS indicates the file separator (so if your file is delimited with a different character e.g. ":" you would have FS=":" within the awk command) and "$2" indicates the 2nd column.

Tuesday, May 19, 2009

Setting a proxy server in Tomcat

Add the following lines to the catalina.properties file (N.B. These settings are for Unix systems. It may be different for Windows.)

http.proxyHost=[proxy ip address or dns]
http.proxyPort=
[proxy port]
http.nonProxyHosts=
[all hosts for which a proxy is not required (delimit each host with a '|']

Example:
http.proxyHost=10.113.49.49
http.proxyPort=8080
http.nonProxyHosts=localhost|10.*

Wednesday, May 6, 2009

scp: ambiguous target

I was attempting to secure copy a file over a remote server and I encountered the following error:
$ scp file.txt user@ip_address:"/file path/"
scp: ambiguous target

So after some trial and error I discovered the problem was the space " " in the path to which I was attempting to copy the file to (despite having put the path within quotes).

To solve this problem you need to escape the space e.g.
$ scp file.txt user@ip_address:"/file\ path/"

Tuesday, April 7, 2009

Running a command in unix that continues to run when you logout

It is often necessary to run a script/process in the background that continues to run when you logout. In Unix/Linux systems we make use of the nohup command e.g.
nohup java [java_class] &

The "&" indicates that the process should run in the background.

Tuesday, February 17, 2009

Counting the occurance of a string in a file

The following counts the number of lines the occurrence of the word "test" occurs in a file:
grep -c " test " filename

Counting all occurrences:
grep -o " test " filename | wc -w

Wednesday, January 28, 2009

Going through a file line by line in java

BufferedReader inFile = new BufferedReader("filename");
String line = null;
try {
   while ((line=inFile.readLine())!=null) {
      //do something with the line here
   }
}
catch (IOException ioe) {
   ioe.printStackTrace();
}

Monday, January 26, 2009

Converting a java.util.Date object into a java.sql.Date object


public class ConvertDate {
   public java.sql.Date convertDate(java.util.Date date) {
      return new java.sql.Date(date. getTime() );
   }
}

Monday, January 19, 2009

Some basic SED commands

'sed' is the search replace command that can be used in Linux.

Here are some examples on how to use 'sed':


sed 's/ //g'
- remove the spaces from the file. The 'g' indicates that the spaces should be replaced globally.

sed 's/cat/dog/g' - replace all instances of the word 'cat' with the word 'dog' in the file.

sed 's/2008-...../&,/g' - search for '2008-' followed by any 5 characters and append a ',' to the end of the string. The '&' indicates what was searched for which in this case was '2008-.....'.

All of the above examples can be used to alter a file and written to a new file e.g. cat filename | sed 's/2008/2007/g' > new_filename

Tuesday, January 6, 2009