Search This Blog

Wednesday, September 09, 2009

print a file by line numbers

Consider the file

$ cat name.txt
1 k
2 ka
3 kam
4 kama
5 kamar
6 kamara
7 kamaraj
8 kamaraju
9 kamaraju
10 kamaraju k
11 kamaraju ku
12 kamaraju kus
13 kamaraju kusu
14 kamaraju kusum
15 kamaraju kusuma
16 kamaraju kusuman
17 kamaraju kusumanc
18 kamaraju kusumanch
19 kamaraju kusumanchi

To print the contents of lines between 5 and 8

$ sed -n '5,8p' name.txt
5 kamar
6 kamara
7 kamaraj
8 kamaraju

$echo $?
0

To print the lines 10 to 20, just do

$ sed -n '10,20p' name.txt
10 kamaraju k
11 kamaraju ku
12 kamaraju kus
13 kamaraju kusu
14 kamaraju kusum
15 kamaraju kusuma
16 kamaraju kusuman
17 kamaraju kusumanc
18 kamaraju kusumanch
19 kamaraju kusumanchi

$echo $?
0

Note that this command is successful even though the specified right limit (20) is greater than the number of lines in the file (19).

To print the nth line of a file (w.l.g. say n=8), just do

$ sed -n 8p name.txt
8 kamaraju

$ echo $?
0

End of line is represented by $. So, to print from 17th line till the end of the file, do

$ sed -n '17,$p' name.txt
17 kamaraju kusumanc
18 kamaraju kusumanch
19 kamaraju kusumanchi

$ echo $?
0

tags | print specific lines in a file

Saturday, June 20, 2009

No theme index file.dpkg error

While trying to upgrade to texmacs 1:1.0.7.2-1in Sid (unstable) on a machine running Debian Lenny (Stable), I was getting the following error

$sudo apt-get install texmacs
// bunch of apt-get messages

Setting up texmacs-common (1:1.0.7.2-1) ...
gtk-update-icon-cache: No theme index file.dpkg: error processing texmacs-common (--configure):
subprocess post-installation script returned error exit status 1
dpkg: dependency problems prevent configuration of texmacs:
texmacs depends on texmacs-common (= 1:1.0.7.2-1); however:
Package texmacs-common is not configured yet.
dpkg: error processing texmacs (--configure):
dependency problems - leaving unconfigured
Errors were encountered while processing:
texmacs-common
texmacs
E: Sub-process /usr/bin/dpkg returned an error code (1)


Install the gnome-icon-theme package to solve this bug.
$sudo apt-get install -t stable gnome-icon-theme
//bunch of apt-get messages

Selecting previously deselected package gnome-icon-theme.
(Reading database ... 225097 files and directories currently installed.)
Unpacking gnome-icon-theme (from .../gnome-icon-theme_2.22.0-1_all.deb) ...
Setting up texmacs-common (1:1.0.7.2-1) ...
gtk-update-icon-cache: Cache file created successfully.
Setting up texmacs (1:1.0.7.2-1) ...
Setting up gnome-icon-theme (2.22.0-1) ...

Thursday, June 04, 2009

running external commands

  1. To run external commands while editing a file in vim, use the '!' in the normal mode. For example
    :!ls -al
    will list the files
    :!date
    will display the current date.

  2. To read the output of external commands into the current file, do
    :r !date
    All the commands are run in normal mode. Press ESC key to enter the normal mode in vim.

    Further reading:- :help :!

  3. To run external commands in octave, use the system command. Sample octave session looks as
    $octave -q
    octave:1> system("date")
    Thu Jun 4 23:49:23 EDT 2009
    ans = 0
    octave:2> [ret_code output] = system("date");
    octave:3> ret_code
    ret_code = 0
    octave:4> output
    output = Thu Jun 4 23:49:40 EDT 2009

    octave:5> exit
    Further reading :- "doc system" shows the relevant help pages in octave.

  4. To run external commands in Fortran 90 programs, use the system command. Sample code looks as below
    $cat system.f90
    program callsystem
    implicit none
    !to examine the behaviour of the system command
    character (len=100)::cmd
    cmd="echo Wake up Neo"
    !if u are using ifc compiler use -Vaxlib during compilation
    call system(cmd//achar(0))
    call system(cmd)
    call system("date")
    ! The next line also works.
    ! call system("ls")
    end program callsystem

    $gfortran system.f90

    $./a.out
    Wake up Neo
    Wake up Neo
    Thu Jun 4 23:55:07 EDT 2009
  5. To run the external commands in C, use the system command available in stdlib.h. Sample code will be
    $cat system.c
    #include stdio.h
    #include stdlib.h

    int main() {
    /* Fixme :- <, > in the header files are not showing up on blogspot */
    int ret_code;
    ret_code = system("date");

    printf("%d\n", ret_code);
    return 0;
    }

    $gcc -Wall system.c

    $./a.out
    Fri Jun 5 00:04:14 EDT 2009
    0

    Further reading :- man system

    All the above are tested in Debian Lenny using vim 7.1, octave 3.0.1, gfortran 4.3.1, gcc 4.3.1

Thursday, April 23, 2009

file test operators in bash

Often while writing shell scripts, various tests need to be performed on files. For example, we need to check if a file exists before copying it somewhere. We need to check the existence of a directory before writing files into it.

Bash comes with the following list of operators to perform these tests.
Operator    Tests Whether
-e File exists
-f File is a regular file
-d File is a directory
-h File is a symbolic link
-L File is a symbolic link
-b File is a block device
-c File is a character device
-p File is a pipe
-S File is a socket
-t File is associated with a terminal

-N File was modified since last read
-O You own the file
-G Group id of the file is same as yours

-s File is not zero size

-r File has read permission
-w File has write permission
-x File has execute permission

-g sgid flag set
-u suid flag set
-k "sticky bit" set

F1 -nt F2 File F1 is newer than F2 *
F1 -ot F2 File F1 is older than F2 *
F1 -ef F2 Files F1 and F2 are hard links to the same file *


! NOT (inverts the sense of above tests)

* signifies a binary operator (requires two operands).


Reference :- Appendix B of "Advanced Bash-Scripting guide" by Mendel Cooper, Version 6.5. Debian users can get this document by installing the abs-guide package.
sudo apt-get install abs-guide

The necessary files can be found in /usr/share/doc/abs-guide . However, the abs-guide package contains only the html version of the document. A pdf version can be downloaded from http://www.tldp.org/LDP/abs/abs-guide.pdf .

Monday, March 30, 2009

script to get the external IP address

When a machine sits behind a router, it has two IP addresses. One internal (assigned by the router), one external (assigned by the ISP). To find the external IP address of the machine, I use the following script called ip.
$cat ip
#! /bin/sh

# get the external IP address of the machine
# Author : Kamraju Kusumanchi
# Date : Mon Mar 30 00:08:11 EDT 2009

curl www.whatismyip.org
echo ""
On my machine, I have
$curl --version
curl 7.18.2 (i486-pc-linux-gnu) libcurl/7.18.2 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/0.6.5 libssh2/0.18
Protocols: tftp ftp telnet dict ldap ldaps http file https ftps scp sftp
Features: GSS-Negotiate IDN IPv6 Largefile NTLM SSL libz
Execution
$which ip
/home/rajulocal/bin/ip

$ip
141.153.242.115

Saturday, February 14, 2009

IP spoofing with iceweasel

This tutorial explains how to spoof the IP addresses using iceweasel (firefox) web browser on machines running Debian Linux.

  1. Install the Iceweasel browser, tor button extension
    $sudo apt-get install iceweasel iceweasel-torbutton
  2. Install the privoxy, tor packages
    $sudo apt-get install privoxy tor
  3. Configure privoxy by adding the following line to /etc/privoxy/config
    forward-socks4a / 127.0.0.1:9050 .
    Note the '.' at the end. It is important.

  4. Restart the privoxy
    $sudo /etc/init.d/privoxy restart
    Restarting filtering proxy server: privoxy.
  5. Restart the tor daemon
    $sudo /etc/init.d/tor restart
    Stopping tor daemon: tor.
    Raising maximum number of filedescriptors (ulimit -n) to 32768.
    Starting tor daemon: tor...
    Feb 14 00:14:49.473 [notice] Tor v0.2.0.30 (r15956). This is experimental software. Do not rely on it for strong anonymity. (Running on Linux i686)
    Feb 14 00:14:49.474 [notice] Initialized libevent version 1.3e using method epoll. Good.
    Feb 14 00:14:49.474 [notice] Opening Socks listener on 127.0.0.1:9050
    done.
  6. Start the iceweasel. Use ctrl+2 to enable tor. The status of the tor button is visible in the right bottom box of the iceweasel window. The screenshots below might be helpful.





    Note: ctrl+2 acts as a toggle switch to enable/disable tor.

  7. Change the proxy. Right click on the tor button (bottom right corner of iceweasel) -> choose preferences -> select "use custom proxy settings"



  8. Click on "Test Settings" then click "ok" to test for proxy settings. If everything went successful, there will be a confirmation.



  9. Check the new spoofed "IP address" by visiting sites such as www.whatismyip.com etc., This will be the IP address seen by the websites you visit.

    Tor has its limitations. It will not give complete anonymity but something good enough for most purposes. Use at your own risk.

The above tutorial is tested in Debian Lenny, iceweasel 3.0.5, tor 0.2.0.30, privoxy 3.0.9, torbutton 1.2.0.

Back ground story :- While hopping around the internet, I came across some articles which claim to track the "identity thefters" via the IP address hits to a website. This is a good approach but we have to understand that it has its own limitations. Using this article, I just wanted to point out that it is very easy to hide one's IP address tracks. Dont be misled by the IP hits...

Followers