Monday, July 11, 2011

inserting at a particular line no shell

content of hello.data file before insertion:

acer@ubuntu:/tmp$ cat hello.data
hello
how are you
wonderful
great awesome
May be might be

Script file:

acer@ubuntu:/tmp$ cat dd.sh
#!/bin/bash
sed -i '5ikarthik' /tmp/hello.data 

acer@ubuntu:/tmp$ ./dd.sh

Content of hello.data file after insertion:

acer@ubuntu:/tmp$ cat hello.data
hello
how are you
wonderful
great awesome
karthik
May be might be
acer@ubuntu:/tmp$

Reading a file and writing to stdout shell

content of hello.dat file:

acer@ubuntu:/tmp$ cat hello.data
hello
how are you
wonderful
great awesome
May be might be
acer@ubuntu:/tmp$ 

content of script file:

acer@ubuntu:/tmp$ cat hh.sh
#!/bin/bash
while read line ;do
echo $line ;
done < /tmp/hello.data
acer@ubuntu:/tmp$
OUTPUT:

acer@ubuntu:/tmp$ ./hh.sh
hello
how are you
wonderful
great awesome
May be might be

Converting lowercase to uppercase and uppercase to lowercase

content of perl_case.pl:

$name="HhEesssSSSSS";
#converting into uppercase
$capital=uc($name);
print "the name in uppercase is $capital.\n";
$lowercase=lc($name);
print "the name in lowercase is  $lowercase.\n";

OUTPUT:

G:\PERL_P~1>perl perl_case.pl
the name in uppercase is HHEESSSSSSSS.
the name in lowercase is  hheessssssss.
G:\PERL_P~1>

Writing to a file perl

perl_fileout.pl:

open(FILE,">>hello_out.dat");
print FILE "hello howw are you";
print FILE "wonderful";
print FILE "awesome";
print FILE "great";
close(FILE);
G:\perl_programs>perl perl_fileout.pl
G:\perl_programs>


When you run this program hell_out.dat is created in the current directory  with the following content:

hello howw are youwonderfulawesomegreat

the following program for making lines come in a new line:
open(FILE,">>hello_out.dat");
print FILE "hello howw are you"."\n";
print FILE "wonderful"."\n";
print FILE "awesome"."\n";
print FILE "great"."\n";
close(FILE);

Reading a File and printing to STDOUT Perl

content of hello.dat:

hello how are you
wonderful beautiful
great
award great
awesome

content of Perl file:

perl_file1.pl
open(FILE,"<hello.dat");
@lines=<FILE>;      
close(FILE);
foreach(@lines)
{
chop;
print;   # it is same as print $_;
print "\n";
}

output:

G:\perl_programs>perl perl_file.pl
hello how are you
wonderful beautiful
greataward great
awesom
G:\perl_programs>