Tuesday, December 15, 2009

How to change boot order in GRUB2?

With Ubuntu 9.10 (Karmic Koala), GRUB2 is the default boot loader.
How to change boot order in GRUB2?

1.
$ cat /etc/grub/grub.cfg
see the order of the wanted kernel. Starts from 0.

2.
$ vi /etc/default/grub
change GRUB_DEFAULT=0 value to wanted kernel

3. run
$ update-grub
to update

4. reboot and check with
$ uname -r
to see if correct kernel selected.

Monday, December 7, 2009

Find Biggest Files/Directories





Since ls does not give correct size on disk, better to
use du command.





$ du /path/to/dir | sort -nr




I hope above
command will give you the proper result you wanted.



Note: The above command may take large time, depending on

number of files in /path/to/dir . You can use depth(say 2) in
that dir.




$ du --max-depth=2 /path/to/dir | sort -nr






Tuesday, November 17, 2009

Newer Yahoo! Messenger Protocol on Pidgin on Debian Lenny 5.0

Yahoo! has changed authentication method of Yahoo! messenger protocol, which is not supported in Pidgin (Version <>= 2.5.7) supports newer Yahoo! Messenger Protocol. But Debian 5.0 Lenny repository has older pidgin (2.4.3). We can install newer pidgin, in the following steps:

## download latest pidgin from pidgin.im (at this time 2.6.3 is latest)
$ wget http://sourceforge.net/projects/pidgin/files/Pidgin/pidgin-2.6.3.tar.bz2

## extract files using tar
$ tar -zxvf pidgin-2.6.3.tar.bz2

$ cd pidgin-2.6.3

## Configure, Compile, and make
$ ./configure --disable-screensaver --disable-vv --disable-avahi --disable-tcl --disable-tk --prefix=/usr
$ make
$ sudo make install


Now you can use newer pidgin with newer Yahoo! Messenger. :)
Enjoy Yahoo!

Find Files Created Between 2 Times

In order to find files created between two times (start hour and end hour). The required hours are hours from current time. For example,

Current time = 2 PM = 14:00

If you want file created between 9 AM and 12 PM today, the start hour and end hour are:

Start Hour = (14 - 9) = 5
End Hour = (14 - 12) = 2

Hence required command is:


$ ./find_files_between_times.pl /path/to/dir 5 2



The Perl script which does this is given below:-


#!/usr/bin/perl -w
#===============================================================================
#
# FILE: find_files_between_times.pl
#
# USAGE: ./find_files_between_times.pl <dir> <start_hour> <end_hour>
#
# DESCRIPTION:
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Mitesh Singh Jat (mitesh), <mitesh[at]yahoo-inc[dot]com>
# COMPANY: Yahoo Inc, India
# VERSION: 1.0
# CREATED: 11/13/2009 10:00:02 PM IST
# REVISION: ---
#===============================================================================

use strict;
use warnings;

if (@ARGV != 3)
{
print STDERR "Usage: $0 <dir> <start_hour> <end_hour>\n";
exit(-1);
}

my $dir = $ARGV[0];
## Calculate current_hour - given_hour
#my $start_time = `/bin/date "+\%H"`;
my $start_time = $ARGV[1];
#chomp($start_time);
my $end_time = $ARGV[2];
$end_time = time - ($end_time * 60 * 60);
#$end_time = $start_time - $end_time;
#$start_time = $start_time - $ARGV[0];

if ($start_time > $end_time)
{
($start_time, $end_time) = ($end_time, $start_time);
}

my $cmd = "find $dir -ctime -$start_time";
print "Running $cmd\n";
my @files = `$cmd`;
foreach my $file (@files) # (`find $dir -ctime $start_time`)
{
chomp($file);
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks)
= stat($file);

if ($ctime > $end_time)
{
next;
}
print "$file\n";
}


exit(0);


Monday, October 26, 2009

Better Console Calculator Using bc

If we use expr for mathematical calculations on console (terminal), it is frustrating, and hard to remember syntax and escape sequences used in expr. :( This small tip will help us.

Just add following line in your .bashrc file (~/.bashrc).

function calc
{
echo "${1}" | bc -l;
}

Then update the shell environment by:

$ source ~/.bashrc


"calc" from the shell will work as follows:

$ calc 2+3
5
$ calc 1+2*3
7
$ calc 1.1*2
2.2
$ calc "(1+2)*3"
9
$ calc "(1+2)^3"
27
$ calc "s(.5)"
.47942553860420300027

Friday, October 23, 2009

Checking Network Services Using Telnet

Suppose we want to check whether the web server(port 80) or any other network service is running or not, but we do not have any browsers (lynx, Firefox, IE, Netscape, ...), then there is a simple way of doing that. Just telnet to that machine on the http port (port no. 80 in general).


$ telnet <ip_addr> <port_no>


And then, give command "get /" (without quotes). If the webserver is running, it displays the HTML script of the homepage or basic info and closes the connection to the remote host.



$ telnet wordpress.com 80
Trying 76.74.254.126...
Connected to wordpress.com.
Escape character is '^]'.

get /
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>
Connection closed by foreign host.


Tuesday, October 20, 2009

Joining Multiple Videos into One Using Mplayer

In order to join multiple video files, into a single video file, you just need to issue following command:



$ mencoder -oac copy -ovc copy -o joined_single_video.avi video1.avi video2.avi video3.avi




Even you can use wild cards (*, ?) too.



$ mencoder -oac copy -ovc copy -o joined_single_video.avi video*.avi




PS. In place of 'avi', you can give whatever file format is available.

Monday, October 5, 2009

Converting Improper mp3 into Proper mp3 for Nokia E71

The music player of Nokia E71 is vulnerable to improper mp3. It stucks forever, even if there is a single improper mp3. Improper mp3 is a mp3 file which does not conform to mp3 standard, there are some problem in mp3 headers. For more information on improper mp3, please read manual of checkmp3 command.

$ man checkmp3

I have written a shell script which converts improper mp3's into proper mp3.



#!/bin/bash

## Script to fix mp3 files using checkmp3

## Checking existance of checkmp3
checkmp3=`which checkmp3`

if [[ "$checkmp3" == "" ]]
then
echo "$0: please install checkmp3."
exit
fi

if [ $# -ne 1 ]
then
echo "USAGE: $0 <mp3_file|dir_with_mp3s>"
exit
fi

dir="$1"
file=""

if [[ -d "$dir" ]]
then
echo "processing directory '$dir'"
temp_file="$dir/fixed.mp3"
for file in `find "$dir" -iname "*.mp3" | sed 's/ /\\\_/g'`
do
file=`echo "$file" | sed 's/\\\_/ /g'`
echo "processing file '$file'"
$checkmp3 -i -sf "$file" > "$temp_file"
if [ $? -ne 0 ]
then
echo "$0: error in processing file '$file'"
else
#eyeD3 "$temp_file"
#$checkmp3 "$temp_file"
mv "$temp_file" "$file"
fi
done
rm -f "$temp_file"
else
file="$dir"
echo "processing file '$dir'"
dir=`dirname "$file"`
temp_file="$dir/fixed.mp3"
echo "$temp_file"

$checkmp3 -i -sf "$file" > "$temp_file"
if [ $? -ne 0 ]
then
echo "$0: error in processing file '$file'"
else
#eyeD3 "$temp_file"
#$checkmp3 "$temp_file"
mv "$temp_file" "$file"
fi
fi
exit 0



Sample run:

$ ./fix_mp3.sh xyz.mp3
$ ./fix_mp3.sh /path/to/mp3/directory/

Wednesday, September 30, 2009

How to Change MAC Address

Changing MAC address of a machine is called spoofing a MAC address or faking a MAC address. In linux, you can change MAC address of your machine. This is how it is done.

First find the physical MAC address of your machine by running the following command :

$ ifconfig -a | grep HWaddr
eth0 Link encap:Ethernet HWaddr 00:1f:f3:cc:c2:f9

The hexadecimal numbers in blue denote my machine's MAC address. Yours will be different.

Next, login as root in Linux and enter the following commands -



# ifconfig eth0 down
# ifconfig eth0 hw ether 00:11:22:33:44:55
# ifconfig eth0 up
# ifconfig eth0 | grep HWaddr


Note above that I have changed the MAC address to a different number highlighted in blue. 00:11:22:33:44:55 is the new MAC address I have provided for my Linux machine. You can choose any 48 bits hexadecimal address as your MAC address.

Thursday, September 10, 2009

Disabling Macbook Pro Touchpad

We may want to disable Macbook Pro (or any other Laptop) Touchpad, such cases are:
(i) We have connected a USB mouse, so we do not want to use touchpad for now.
(ii) While typing, there is no use of touchpad, if touchpad is too sensitive, the cursor keeps on jumping due to slight touch of palm or finger.

I have written a script to disable/enable touchpad (which is using synaptics driver).





#!/bin/bash

## Disable touchpad if USB Mouse is attached

SYNAPTICS=`which synclient`

if [[ "$SYNAPTICS" == "" ]]
then
echo "$0: please install synaptics touchpad driver."
echo "Also make sure that 'Option \"SHMConfig\" \"on\"'"
echo " is added in Touchpad device Section in /etc/X11/xorg.conf"
exit
fi

USB_mouse_present=`grep -ic "usb.*mouse" /proc/bus/input/devices`
# if no USB Mouse; enable touchpad
if [ $USB_mouse_present -eq 0 ]
then
$SYNAPTICS TouchpadOff=0
else
$SYNAPTICS TouchpadOff=1
fi

# if any parameter [on|off] is given, override previous command
if [ $# -ge 1 ]
then
if [ "$1" = "on" ]
then
$SYNAPTICS TouchpadOff=0
else
$SYNAPTICS TouchpadOff=1
fi
fi

exit 0



Sample Run:
Turn on touchpad

$ ./touchpad.sh on

Turn off touchpad

$ ./touchpad.sh off

If we have plugged USB mouse, then just give following command

$ ./touchpad.sh

On removing USB mouse, give following, the touchpad will be enabled automatically. :)

$ ./touchpad.sh


PS: The configuration for synaptics driver can be referred from here or here.

Friday, September 4, 2009

IP Masquerade and Network Address Translation (NAT)

If we want to connect multiple computers to the Internet using single public IP Address, Masquerading (A form of NATing) helps us.

NAT describes the process of modifying the network addresses contained with datagram headers while they are in transit. IP masquerade is the name given to one type of network address translation that allows all of the hosts on a private network to use the Internet at the price of a single IP address.

IP masquerading allows you to use a private (reserved) IP network address on your LAN and have your Linux-based router perform some clever, real-time translation of IP addresses and ports. When it receives a datagram from a computer on the LAN, it takes note of the type of datagram it is, “TCP,” “UDP,” “ICMP,” etc., and modifies the datagram so that it looks like it was generated by the router machine itself (and remembers that it has done so). It then transmits the datagram onto the Internet with its single connected IP address. When the destination host receives this datagram, it believes the datagram has come from the routing host and sends any reply datagrams back to that address. When the Linux masquerade router receives a datagram from its Internet connection, it looks in its table of established masqueraded connections to see if this datagram actually belongs to a computer on the LAN, and if it does, it reverses the modification it did on the forward path and transmits the datagram to the LAN computer.

I have written a shell script, which converts a Linux box into a router. The script is written as:




#!/bin/bash

## Output interface: connected to Internet
out_iface=ppp0

## Run as root always
user_id=`whoami`

if [[ "$user_id" != "root" ]]
then
echo "$0: please run this script as root user."
exit
fi

## Checking existance of iptables
IPTABLES=`which iptables`

if [[ "$IPTABLES" == "" ]]
then
echo "$0: please install iptables."
exit
fi

if [ $# -ge 1 ]
then
case "$1" in
status)
$IPTABLES -t nat -L
exit 0
;;
stop)
## Disabling Packet forwarding in kernel
echo 0 > /proc/sys/net/ipv4/ip_forward
echo "Flushing NAT MASQUERADE Entries"
$IPTABLES -t nat -F
exit 0
;;
restart)
$0 stop
if [ $# -ge 2 ]
then
$0 start $2
else
$0 start
fi
;;
start)
if [ $# -ge 2 ]
then
out_iface=$2
fi
## Enabling Packet forwarding in kernel
echo 1 > /proc/sys/net/ipv4/ip_forward

## Enabling NAT Masquerade, if not enabled
if [ -z "`$IPTABLES -t nat -L | grep MASQUERADE`" ]
then

$IPTABLES -t nat -A POSTROUTING -o $out_iface -j MASQUERADE
fi
;;
*)
echo "USAGE: $0 <start|status|restart|stop> [internet_interface]"
exit 1
;;
esac
else
echo "USAGE: $0 <start|status|restart|stop> [internet_interface]"
exit 1
fi

exit 0



Sample Runs:

$ ./NAT_Masquerade.sh
./NAT_Masquerade.sh: please run this script as root user.
$ sudo ./NAT_Masquerade.sh
USAGE: ./NAT_Masquerade.sh [internet_interface]
$
Here, internet_interface is the interface which is connected to internet.
By default, ppp0 (Dial up) interface is taken.

$ sudo ./NAT_Masquerade.sh status
Chain PREROUTING (policy ACCEPT)
target prot opt source destination

Chain POSTROUTING (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination
$
Since Masquerade is not yet applied, Chain POSTROUTING rule is empty.

Applying IP Masquerade to internet_interface eth0.
$ sudo ./NAT_Masquerade.sh start eth0
$ sudo ./NAT_Masquerade.sh status
Chain PREROUTING (policy ACCEPT)
target prot opt source destination

Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
MASQUERADE all -- anywhere anywhere

Chain OUTPUT (policy ACCEPT)
target prot opt source destination
$ sudo ./NAT_Masquerade.sh stop
Flushing NAT MASQUERADE Entries
$

Monday, August 31, 2009

Fixing Wired Network (eth0) Ethernet

While using Debian based systems(like Ubuntu), I often found that network manager just got into the way whenever I wanted to fix an internet connection. So, first thing to do is to kill the associated processes (kill -9) after you find the process ID by using ps. Or you can totally get rid of these packages (usually network-manager, network-manager-gnome in GNOME or knetworkmanager in KDE).

Then, edit /etc/network/interfaces file.

$ sudo vi /etc/network/interface

auto eth0

iface eth0 inet dhcp

again assuming that we want to have a DHCP based connection.

Then restart the network connection by issuing the following command:

$ sudo /etc/init.d/networking restart

Friday, August 14, 2009

Determining Bits of CPU and OS (32/64-bits)

In order to determine whether OS/CPU is 32-bit or 64-bit,
you can use this shell script.


#!/bin/bash
echo -n "Running "

RES=`uname -a | grep 64`
if [ $? -eq 0 ]; then

echo -n "64-bit "
else
echo -n "32-bit "
fi
echo -n "operating system on a "

RES=`cat /proc/cpuinfo | grep " lm "`
if [ $? -eq 0 ]; then

echo -n "64-bit "
else
echo -n "32-bit "
fi
echo "machine"




Sample Run:

$ ./os_cpu.sh
Running 64-bit operating system on a 64-bit machine

Wednesday, August 5, 2009

Let Linux Speak Time and Day for You

I have written a single line command that tells time of the day:

$ espeak "Time is `/bin/date \"+%H
hours %M minutes %S seconds\"`"




You need not to type this command every time, just run the

following commands only one time:


$ echo "alias speak_time='espeak \"Time is \`/bin/date \"+%H hours %M minutes %S seconds\"\`\"'" >> ~/.bashrc

$ source ~/.bashrc




Now, you will be able to run speak_time any number of time at any time, and you will get exact time. :-)


$ speak_time



PS: Requirement of above command is "espeak"


$ sudo apt-get install espeak


For speaking day, just add following alias in your .bashrc file

alias speak_day='espeak "Today is `/bin/date \"+%A, %d %B 20%y\"`"'

Monday, July 27, 2009

Mount Windows Shared Folder onto Linux Directory

Suppose you want to mount Windows shared folder (say shared_folder) on windows machine (IP address 10.226.194.169) on a folder (say /local/smbshare/shared_folder), you just need to give following
command:


$ sudo mount -t smbfs -o username=domainname
\\yourname,password=yoursecretpassword -o gid=users,dmask=777,fmask=777,rw //10.226.194.169/shared_folder /local/smbshare/shared_folder

You can verify whether the above command is successful or not, by using:
$ mount | grep shared_folder

If the above command shows some output, it means mount is successful :)

Sunday, July 12, 2009

Mounted Filesystem Information

For getting information of a partition (filesystem) which is mounted on a directory on Linux System, I have created a C program. Its listing is given as below:





/*
* =====================================================================================
*
* Filename: file_system_stat.c
*
* Description: Get file system statistics of mounted partition
*
* Version: 1.0
* Created: Thursday 09 July 2009 10:48:02 IST IST
* Revision: none
* Compiler: gcc
*
* Author: Mitesh Singh Jat (mitesh)
*
* =====================================================================================
*/

#include <stdio.h>
#include <sys/statfs.h>

int file_system_stats(const char *pathp)
{
struct statfs sfs;
int retval;
if (pathp == NULL)
{
fprintf(stderr, "file_system_stats: cannot find stats of NULL path\n");
return (-1);
}
retval = statfs(pathp, &sfs);
if (retval < 0)
{
fprintf(stderr, "file_system_stats: cannot find file system\n");
fprintf(stderr, " stats of partition %s\n", pathp);
return (-1);
}
printf("\n=== Filesystem Stats of '%s' ===\n", pathp);
printf("Optimal Transfer Block Size = %ld\n", sfs.f_bsize);
printf("Total data blocks = %ld\n", sfs.f_blocks);
printf("Total free data blocks = %ld\n", sfs.f_bfree);
printf("Total file nodes = %ld\n", sfs.f_files);
printf("Total free file nodes = %ld\n", sfs.f_ffree);
printf("=================================================\n\n");
return (retval);
}

int main(int argc, char *argv[])
{
int i;
int retval = 0;
if (argc <= 1)
{
fprintf(stderr, "Usage: %s <path> [more_paths...]\n", argv[0]);
retval = file_system_stats("./");
return (retval);
}

for (i = 1; i < argc && retval >= 0; ++i)
{
retval = file_system_stats(argv[i]);
}
return (retval);
}



Compilation:
$ gcc -Wall -o file_system_stat file_system_stat.c

Sample Runs:
--(mitesh@roundduck-lm)-(~/Programming/C/Usp)--
--(0 : 505)
$ ./file_system_stat
Usage: ./file_system_stat <path> [more_paths...]


=== Filesystem Stats of './' ===
Optimal Transfer Block Size = 4096
Total data blocks = 9690330

Total free data blocks = 3956097

Total file nodes = 2449408

Total free file nodes = 2315870

=================================================


--(mitesh@roundduck-lm)-(~/Programming/C/Usp)--

--(0 : 506)
$ ./file_system_stat /boot

=== Filesystem Stats of '/boot' ===

Optimal Transfer Block Size = 4096

Total data blocks = 12017122

Total free data blocks = 5616343

Total file nodes = 3055616

Total free file nodes = 2830533

=================================================


--(mitesh@roundduck-lm)-(~/Programming/C/Usp)--
--(0 : 507)
$ ./file_system_stat /boot /mnt/extra

=== Filesystem Stats of '/boot' ===

Optimal Transfer Block Size = 4096

Total data blocks = 12017122

Total free data blocks = 5616343

Total file nodes = 3055616

Total free file nodes = 2830533

=================================================



=== Filesystem Stats of '/mnt/extra' ===

Optimal Transfer Block Size = 4096

Total data blocks = 21235766

Total free data blocks = 7269970

Total file nodes = 5357568

Total free file nodes = 5357279

=================================================


--(mitesh@roundduck-lm)-(~/Programming/C/Usp)--

--(1 : 508)
$ ./file_system_stat /this_is_not_a_partition
file_system_stats: cannot find file system

stats of partition /this_is_not_a_partition





Note: By the way, the block size of a filesystem can be found using commands given at here or here.

Wednesday, July 1, 2009

Removing Files With Unusual Characters

Suppose, some one (miscreant) got access to your computer and
creates files, which you may not be able to delete or you will end
up deleting all the files/subdirectories of the directory on which
these files are created.

Some examples of such dangerous file names are:
$ ls -l
total 0K
-rw-r--r-- 1 mitesh users 0 2009-07-01 19:19 *
-rw-r--r-- 1 mitesh users 0 2009-07-01 19:20 -rf *
-rw-r--r-- 1 mitesh users 0 2009-07-01 19:27 -rw-r--r--
-rw-r--r-- 1 mitesh users 0 2009-07-01 19:19 test

Now, how are you going to delete above files?

(1) * : If one tries
$ rm *
or
$ rm -r *
This will delete all the files(& all subdirectories) in present working
directory.
Solution:
$ rm -f ./'*'
We can verify this.
$ ls -l
total 0K
-rw-r--r-- 1 mitesh users 0 2009-07-01 19:20 -rf *
-rw-r--r-- 1 mitesh users 0 2009-07-01 19:27 -rw-r--r--
-rw-r--r-- 1 mitesh users 0 2009-07-01 19:19 test

(2) -rf *: If one tries
$ rm -rf *
This will delete all the files & all subdirectories in present working
directory.
Solution:
$ rm -f ./'*'
We can verify this.
$ ls -l
total 0K
-rw-r--r-- 1 mitesh users 0 2009-07-01 19:27 -rw-r--r--
-rw-r--r-- 1 mitesh users 0 2009-07-01 19:19 test

(3) -rw-r--r--: If one tries
$ rm -rw-r--r--
rm: invalid option -- w
Try `rm ./-rw-r--r--' to remove the file `-rw-r--r--'.
Try `rm --help' for more information.
Solution:
$ rm ./-rw-r--r--
Other Solution
$ rm -- -rw-r--r--

Note: So we need to be vigilant, while using 'rm' command.

Friday, June 26, 2009

Finding Block Size of Filesystem

In order to, find block size of a filesystem on Linux,

$ sudo tune2fs -l /dev/sda1 | grep -i 'block size'
Block size: 4096

OR

$ echo "mitesh"> test && du test | awk '{print $1}' && rm -f test
4K

Thursday, June 25, 2009

Editing Very Very Large File

Suppose we want to do changes in few lines in a very very large file. It is not possible to open such a big file(say size in GBs > RAM+Swap size) in a editor. Even sed/awk takes very long time, because they do pattern matching if mentioned on every line, otherwise, we can do one line editing with a line number. I have written a Perl Script to edit multiple lines independently. It uses sed commands to edit a line.

Format of Config file is:
line_number:sed_command


#!/usr/bin/perl -w
#===============================================================================
#
# FILE: ed_large_file.pl
#
# USAGE: ./ed_large_file.pl <config_file> <file_name> [overwrite}
#
# DESCRIPTION: Edit very[ very] large file
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Mitesh Singh Jat (mitesh), <mitesh[at]yahoo-inc[dot]com>
# VERSION: 1.0
# CREATED: Thursday 25 June 2009 02:32:37 IST IST
# REVISION: ---
#===============================================================================

use strict;
use warnings;

if (@ARGV < 2)
{
print STDERR "$0: <config_file> <file_name> [overwrite]\n";
print STDERR "!!!Be careful while using [overwrite] option,\n";
print STDERR "because original file will be deleted.\n";
exit(-1);
}

my $conf_file = $ARGV[0];
my $large_file = $ARGV[1];
my $overwrite = 0;
if (@ARGV >= 3 && $ARGV[2] eq "overwrite")
{
$overwrite = 1;
}

my $temp_file = `dirname $large_file`;
chomp($temp_file);
if ($temp_file eq "" || (!(-d $temp_file)))
{
print STDERR "$0: Cannot find dirname for temporary file.\n";
print STDERR "Please check path of file '$large_file'\n";
exit(-1);
}

$temp_file = $temp_file . "/temp";
print "Temporary file is '$temp_file'\n";

## Read config file
print "Reading config file '$conf_file'\n";
open(CFH, "$conf_file") or die("Cannot read Config file '$conf_file'\n");
my $line;
my %lineno_sedcmd;
while ($line = <CFH>)
{
chomp($line);
my ($lineno, $sedcmd) = split /:/, $line, 2;
if (defined($sedcmd))
{
$lineno_sedcmd{$lineno} = $sedcmd;
print "$lineno $lineno_sedcmd{$lineno}\n";
# Verifying sedcmd before running it;
# it gives a chance to reedit config file
my $cmd = "echo \"Mitesh Singh Jat\" | sed '$sedcmd' 1> /dev/null 2>&1";
if (!(system($cmd) == 0))
{
print STDERR "$0: sed command '$sedcmd' for line '$lineno'";
print STDERR "is having error. Please recheck with \$ man sed\n";
close(CFH);
exit(-1);
}
}
}
close(CFH);

my @line_nos;
foreach (sort keys (%lineno_sedcmd))
{
push(@line_nos, $_);
}

## Open large file
open(LFH, "$large_file") or die("$0: Cannot open file '$large_file'");
## Temporary File
open(OFH, ">$temp_file") or die("$0: Cannot create temporary file '$temp_file'");
my $nline = 0;
my $i = 0;
my $end_idx = @line_nos - 1;
print "Processing...";
while ($line = <LFH>)
{
++$nline;
if ($line_nos[$i] == $nline) # now edit
{
++$i; # This config line is over
if ($i > $end_idx)
{
$i = $end_idx;
}
chomp($line);
my $cmd = "echo \"$line\" | sed '$lineno_sedcmd{$nline}'";
#print "$cmd\n";
my $out_line = `$cmd`;
print OFH "$out_line";
print " $nline"; #sleep 1; # to see progress :)
}
else
{
print OFH "$line";
}
}

print "\n";

close(OFH);
close(LFH);

if ($overwrite == 0)
{
print "done\n";
exit(0);
}

## Overwite original file by deleting it and moving temp
print "Overwriting...\n";
my $cmd = "rm -f $large_file \&\& mv $temp_file $large_file";
print "$cmd\n";
system($cmd) == 0
or die("Problem in overwriting. '$cmd' failed: $?\n");
print "done\n";
exit(0);


Sample Run:


--(0 : 618)> ./ed_large_file.pl
./ed_large_file.pl:
<config_file> <file_name> [overwrite]
!!!Be careful while using [overwrite] option,

because original file will be deleted.

--(mitesh@roundduck-lm)-(~/Programming/Perl/Editing_Large_Files)--
--(255 : 619)> cat large_file.txt
Shree Ganeshay Namah
Shri Bharat Singh Jat
Smt Amita Jat
Mitesh Jat
Shikha Jat
Shilpa Jat
This is garbage line. Please delete it.
--(mitesh@roundduck-lm)-(~/Programming/Perl/Editing_Large_Files)--
--(0 : 620)> cat large_file.conf
1:s/^.*$/!!&!!/
4:s/ / Singh /
7:/.*/d
--(mitesh@roundduck-lm)-(~/Programming/Perl/Editing_Large_Files)--
--(0 : 621)> ./ed_large_file.pl large_file.conf large_file.txt
Temporary file is './temp'
Reading config file 'large_file.conf'
1 s/^.*$/!!&!!/
4 s/ / Singh /
7 /.*/d
Processing... 1 4 7
done
--(mitesh@roundduck-lm)-(~/Programming/Perl/Editing_Large_Files)--
--(0 : 622)> cat ./temp
!!Shree Ganeshay Namah!!
Shri Bharat Singh Jat
Smt Amita Jat
Mitesh Singh Jat
Shikha Jat
Shilpa Jat
--(mitesh@roundduck-lm)-(~/Programming/Perl/Editing_Large_Files)--
--(0 : 623)> ./ed_large_file.pl large_file.conf large_file.txt overwrite
Temporary file is './temp'
Reading config file 'large_file.conf'
1 s/^.*$/!!&!!/
4 s/ / Singh /
7 /.*/d
Processing... 1 4 7
Overwriting...
rm -f large_file.txt && mv ./temp large_file.txt
done
--(mitesh@roundduck-lm)-(~/Programming/Perl/Editing_Large_Files)--
--(0 : 624)> cat large_file.txt
!!Shree Ganeshay Namah!!
Shri Bharat Singh Jat
Smt Amita Jat
Mitesh Singh Jat
Shikha Jat
Shilpa Jat
--(mitesh@roundduck-lm)-(~/Programming/Perl/Editing_Large_Files)--
--(0 : 625)>