Wednesday, 5 February 2014

Inserting a line in middle of a file , select,

open(READ,"$ARGV[0]");
open(WRITE,">>sam");
select WRITE;
@s=<READ>;
$last=@s;
for($i=0;$i<@s;$i++)
{
if ($s[$i] =~ m/$ARGV[1]/i )
{
print $s[$i];
$lineno=$i;
}
}
###(pattern)
sub fore
{
for($i=$_[0];$i<$_[1];$i++)
{
print $s[$i]."\n";
}
}
##Insert line

&fore(0,$lineno);
print "$ARGV[2]"."\n";
&fore($lineno,$last);
select STDOUT;
print "Work Successfully finished , Please check the file name 'sam'.\n";

"usage :

perl scriptname "filename where you need to search" "pattern to search" "line to append"

example:
perl script_name  "/etc/passwd"  "drizzle" "hi this is sam"

output will be redirect to a filename "sam"



"git:x:1000:1001:sam,111,211,
233,nothing:/home/git:/bin/bash

jenkins:x:119:128::/var/lib/jenkins:/bin/bash

postfix:x:120:130::/var/spool/postfix:/bin/false

hi this is sam
drizzle:x:121:132:Drizzle Server,,,:/var/lib/drizzle:/bin/false

ebnetd:x:122:65534:ebnetd,,,:/var/lib/ebnetd:/bin/false

memcache:x:123:133:Memcached,,,:/nonexistent:/bin/false"
 
 

No comments:

Post a Comment