Wednesday, 19 February 2014
Monday, 17 February 2014
Thursday, 13 February 2014
Grepping a word using sed (BASH)
cat /etc/passwd
kernoops:x:109:65534:Kernel Oops Tracking Daemon,,,:/:/bin/false
pulse:x:110:119:PulseAudio daemon,,,:/var/run/pulse:/bin/false
rtkit:x:111:122:RealtimeKit,,,:/proc:/bin/false
saned:x:112:123::/home/saned:/bin/false
speech-dispatcher:x:113:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/sh
hplip:x:114:7:HPLIP system user,,,:/var/run/hplip:/bin/false
ntp:x:115:124::/home/ntp:/bin/false
puppet:x:116:125:Puppet configuration management daemon,,,:/var/lib/puppet:/bin/false
clamav:x:117:126::/var/lib/clamav:/bin/false
sshd:x:118:65534::/var/run/sshd:/usr/sbin/nologin
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
cat /etc/passwd | sed -n 's/.*\([a-z]\{13\}\)[[:space:]]\([a-z]*\)[[:space:]]\([a-z]*\).*/\1 \3/p'
configuration daemon
\([a-z]\{13\}\) Configuration --> \1 first matched group pattern
([a-z]*\) daemon --> \2 second matched group pattern
kernoops:x:109:65534:Kernel Oops Tracking Daemon,,,:/:/bin/false
pulse:x:110:119:PulseAudio daemon,,,:/var/run/pulse:/bin/false
rtkit:x:111:122:RealtimeKit,,,:/proc:/bin/false
saned:x:112:123::/home/saned:/bin/false
speech-dispatcher:x:113:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/sh
hplip:x:114:7:HPLIP system user,,,:/var/run/hplip:/bin/false
ntp:x:115:124::/home/ntp:/bin/false
puppet:x:116:125:Puppet configuration management daemon,,,:/var/lib/puppet:/bin/false
clamav:x:117:126::/var/lib/clamav:/bin/false
sshd:x:118:65534::/var/run/sshd:/usr/sbin/nologin
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
cat /etc/passwd | sed -n 's/.*\([a-z]\{13\}\)[[:space:]]\([a-z]*\)[[:space:]]\([a-z]*\).*/\1 \3/p'
configuration daemon
\([a-z]\{13\}\) Configuration --> \1 first matched group pattern
([a-z]*\) daemon --> \2 second matched group pattern
Tuesday, 11 February 2014
Grepping only Feb , 11 th log from auth.log using perl
open("FILE","/var/log/auth.log");
while($line=<FILE>)
{
if($line=~m/(\b[A-Z,a-z]{3}\b)(\s+)(\d+)/)
{
if(($1 eq "Feb" )&&($3==11))
{
print "$line"."\n";
}
}
}
while($line=<FILE>)
{
if($line=~m/(\b[A-Z,a-z]{3}\b)(\s+)(\d+)/)
{
if(($1 eq "Feb" )&&($3==11))
{
print "$line"."\n";
}
}
}
Wednesday, 5 February 2014
Finding uniq and repeated words in perl
%hash=();
open("FILE","/etc/passwd");
while($line=<FILE>)
{
chomp($line);
push (@s,$_) foreach (split(":",$line));
}
foreach (@s)
{
$hash{$_}++;
}
print "Word no of occurance";
foreach $key (keys %hash)
{
print $key."=".$hash{$key}."\n";
}
%hashn=();
foreach (@s)
{
unless(exists $hashn{$_} )
{
$hashn{$_}++;
}
else
{
push (@uniq,$_);
}
}
%hashne=();
foreach (@uniq)
{
$hashne{$_}=0;
}
print "Repeating word\n";
foreach $kys (keys %hashne)
{
print "$kys ";
}
print "\n";
open("FILE","/etc/passwd");
while($line=<FILE>)
{
chomp($line);
push (@s,$_) foreach (split(":",$line));
}
foreach (@s)
{
$hash{$_}++;
}
print "Word no of occurance";
foreach $key (keys %hash)
{
print $key."=".$hash{$key}."\n";
}
%hashn=();
foreach (@s)
{
unless(exists $hashn{$_} )
{
$hashn{$_}++;
}
else
{
push (@uniq,$_);
}
}
%hashne=();
foreach (@uniq)
{
$hashne{$_}=0;
}
print "Repeating word\n";
foreach $kys (keys %hashne)
{
print "$kys ";
}
print "\n";
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"
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,
jenkins:x:119:128::/var/lib/
postfix:x:120:130::/var/spool/
hi this is sam
drizzle:x:121:132:Drizzle Server,,,:/var/lib/drizzle:/
ebnetd:x:122:65534:ebnetd,,,:/
memcache:x:123:133:Memcached,,
Subscribe to:
Posts (Atom)

