Friday, December 26, 2008

Capturing output of terminal on Unix

Terminals do not have a great scroll length and even though you can increase the number of lines that can be scrolled up, it is always useful to have the output captured in a file so that you can search on it. Here are few ways

1. Redirection

cat foo.txt > bar.txt

Outout of foo.txt is captured in bar.txt

2. Redirection with "append"

ls >> bar.txt

This will append the output of "ls" to bar.txt ( Instead of overwriting it)

3. "tee"
Redirect and redirect with append will not display the content on console and hence if you want to see the content and also redirect it to a file , use "tee"

ls | tee bar.txt

this will display the contents of ls on terminal and also redirect it to bar.txt

4. output to multiple files

ls | tee a.txt b.txt c.txt

This will output contents of ls to a.txt, b.txt, c.txt

5. Outout all the terminal interaction to a file

script -f out.txt

Monday, December 22, 2008

Converting UTF-16 to UTF-8

I was running grep on an xml file (big one) and realized that it did not match the pattern I was expecting the the file. I then did a "head" from the file to see a pattern that is present in it. Grepping on that too returned nothing telling me that grep is somehow not working on this file.
"head" was returning some junk characters at the begining of the file - This idicated that the file does not have a normal text encoding and probably that is the reason why grep is not working on it.
Reading the XML header proved that, since it was "UTF-18", grep was not running over it. I had to convert the file to UTF-8 which is more text friendly if the content is only English.

iconv --from-code UTF-16 --to-code UTF-8 input_file.xml > output_file.xml

Saturday, December 13, 2008

screen

when to I use screen :

* When "nohup" is just not enough
* When the power goes off and you are left in the middle of nowhere
* When I need to log on to 10 different terminals each day
* When I do not want to enter 10 different username passwords each day

"screen" is a terrific unix utility that allows you to start a terminal without having to worry about you job being interreputed mid-way. It is better than nohup in that respect nohup only allows unix commands to be started in a "nohup" fashion. If for instance you are working with an interactive program like sqlplus and your internet connection happen to break, you will loose your context and any transactions that you were working on.

It essentially allows you following :
* Multiple sessions in a single ssh window (screen multiplexing)
* Ability to "resume" a session in the state you left it - irrespective of power outage, internet connection termination etc

Here is how you use it :

1. Make sure it is installed on your system by typing "screen --help"
2. If it is not installed, you can install it by "yum install screen" on a fedora box.
3. start a screen session by typing "screen" and you should be in a screen session.
4. If you are disconnected or closed the terminal, type "screen -r -d" to reattach to the session

Here are some of the short-cuts available (All after CTRL+A)

c : creates a new
n : next screen window
p : previous screen window
a : toggle between two screen windows
[0-9] : Go to a specific screen window identified by a number
A : Set the name of a screen window
" : List all the existing screen
ESC : Scroll up or down on a screen

Saturday, November 15, 2008

Man on the moon (well almost)

India becomes the 4th Nation to put a lunar probe on the moon ! I am proud to be the 4th country to do that(as against the alleged 3rd world country banter from Matthew Hayden who knows nothing more than being a sore loser)

Sunday, October 19, 2008

pasting code in vim

:set paste

paste the code

:set nopaste

set pastetoggle=F3 in your vimrc file is an alternative.

Friday, October 17, 2008

Renaming Files in Linux

Type the following on commandline of a bash shell and change the logic as needed.


for i in `ls /home/ssinghal` ; do mv "$i" "$i.new" ; done

Tuesday, September 30, 2008

sudo without password

1. Log in to a unix box as root
2. Execute "visudo" to open sudo editor
3. Enter the following line
ALL=(ALL) NOPASSWD: ALL