Thursday, 16 January 2014

Perl grep ( grepping a line before the pattern match)

($inp=$ARGV[0]) || chomp($inp=<STDIN>);
open("FILE","/etc/passwd");
@s=<FILE>;
foreach $line (@s)
{
$x++;
if( $line=~/$inp/)
{
$d=$x;
}
}
$d=$d-2;
foreach $line (@s)
{
$y++;
if($y==$d)
{
print $line."\n";
}
}

Perl LABEL

DESK:
{
print "bye";
}



print "hit";
goto DESK;


Monday, 13 January 2014

Perl string concatenation

open("FILE","/etc/passwd");
while($line=<FILE>)
{
@s=split(":",$line);
$k.=" # $s[0]";
print $k."\n";
}

Usage:
# root # daemon # bin # sys # sync # games # man # lp # mail # news # uucp # proxy # www-data # backup # list # irc # gnats # nobody # libuuid # syslog # messagebus # colord # lightdm # whoopsie # avahi-autoipd # avahi # usbmux # kernoops # pulse # rtkit # saned # speech-dispatcher #

Saturday, 11 January 2014

Map function in perl

@s=qw( sam bam cam jam );
map { $_ = "$_.so" } (@s);
foreach $i (@s)
{
print $i."\n";
}

Thursday, 9 January 2014

Perl format converter ( symbol and character )

Format:

bin/$OSDIR/(foh) lib/$OSDIR/(libdx_t libmpxref_t libudr_t libudrInt_t libshmadm libudr libudrInt libdx libdta libnatax libmprdm libmputil libmpanonym libmpxref libmpcontainer libnumnorm libmpbasics libtax libbat++ libbat++curcy libbat libcomlock libhooks libudr_sh librulelib libudr_sh_t)

Converted Format:

bin/$OSDIR/foh.so lib/$OSDIR/{libdx_t.so,libmpxref_t.so,libudr_t.so,libudrInt_t.so,libshmadm.so
,libudr.so,libudrInt.so,libdx.so,libdta.so,libnatax.so,libmprdm.so
,libmputil.so,libmpanonym.so,libmpxref.so,libmpcontainer.so,libnumnorm.so,
libmpbasics.so,libtax.so,libbat++.so,libbat++curcy.so,libbat.so,
libcomlock.so,libhooks.so,libudr_sh.so,librulelib.so,libudr_sh_t.so}


Script:

open("FILE","<libsa");
open("FILE1",">>libsa1");

print FILE1 "{";
while($line=<FILE>)
{
$line=~tr/()/{}/;
@s=split(" ",$line);
foreach $i (@s)
{
if ( $i =~ /lib[a-z,A-Z,_]*/ && $i =~ /{lib/ )
 {
    print FILE1 "$i.so,";
 }
elsif ( $i =~ /[a-z,A-z]*/ && $i =~ /{/ && $i =~ /}/ )
 {
    $i=~s/{//;
    $i=~s/}//;
    print FILE1 "$i,";
 }
elsif ( $i =~ /lib[a-z,A-Z,_,.]/ )
 {
    if ( $i =~ /}/ )
    {
    $i=~s/}//;
    print FILE1 "$i.so,"."}";
    }
    else
    {
    print FILE1 "$i.so,";
    }
 }
else
 {
     print FILE1 "$i,";
 }
}

}
print FILE1 "/dev/null}\n";



Same Script in Shell:

cat inputfile | grep -i "(lib[a-x]"  | tr '()' '{}' |  sed -e  's/lib[a-z,A-Z,+,_]*/&.so /g'  -e 's/\s\+/,/g' -e 's/\,\}/}/g' -e 's/\}\,/} /g' -e 's/lib.so,/lib/g' | tr ' ' '\n' | awk 'BEGIN{FS=" "} {if(($0~/{[a-z]*.*}/)&&($0!~/,/)) {gsub(/[{,}]/,"");print} else {print}}'



Wednesday, 8 January 2014

Extracting particular content from xml using perl script

open("FILE","test1.xml");
while($line=<FILE>)
{
@s=split(" ",$line);
foreach $i (@s)
{
if($i=~/\bCT=|\bSN=|\bChType=|\bNI=|\bOAmt=|\bDAmt=/)
{
print $i.";";
}
}
print "\n";
}

Monday, 6 January 2014

Creating path variable in linux

#!/ bin/bash
for i in *
do
[ -d "$i" ] && TEST=$TEST:$i
done
TEST=${TEST#:}
export TEST
echo TEST=$TEST

Sunday, 5 January 2014

Windows Batch script for setting up environment

@echo off
echo > %TEMP%\tpplist.txt
echo > %TEMP%\tppfinal.txt
cls
set dash=#################################################################### 
echo %dash%
echo.
echo Select the 3pp from the below list
echo %dash%
echo.

type  C:\Users\spraveen\Documents\file_list_new.txt
echo.
echo %dash%
echo.

echo Enter the 3pp name
echo.
set /p as=
echo.
echo %dash%
echo.
echo Please select the version that you want to set from the below list  Please type the full name as like in the list or copy paste
echo.
echo %dash%
echo.
type C:\Users\spraveen\Documents\log.txt | findstr /i %as%

if %errorlevel% neq 0 (
echo The 3pp that you have entered is not in the list \n
goto ERROR
)

type C:\Users\spraveen\Documents\applications.txt | findstr /i %as% >> %TEMP%\tpplist.txt

echo.
echo %dash%
echo.
echo Please enter the version name
echo.
echo %dash%
echo.
set /p threever=
echo.
type %TEMP%\tpplist.txt | findstr /i %threever%

if %errorlevel% neq 0 (
echo The version you have entered is not in the list please re-enter properly
goto ERROR
)
echo.
echo %dash%
echo.
echo You have chosen the following version
echo.
echo %dash%
type %TEMP%\tpplist.txt | findstr /i %threever% >> %TEMP%\tppfinal.txt

for /f "tokens=1* delims==" %%u in (%TEMP%\tppfinal.txt) do (
echo %%u
)
echo.
echo %dash%
echo.

echo Confirm to set the version [y/n]
set /p inp=
set trig=false
if "%inp%" == "y" set trig=true
if "%inp%" == "Y" set trig=true
if "%trig%" == "true" (
echo hi
) else (
goto ERROR
)

for /f "tokens=2* delims==" %%v in (%TEMP%\tppfinal.txt) do (
echo %%v
)




:ERROR

Perl password crypt

#!/usr/bin/perl
chomp($line=<STDIN>);
$s=crypt($line,salt);
if ( $s eq 'saUc1YKRDDT5E' )
{
print "You have cracked the code Boss :) \n";
 }



Usage:

perl filename sam

Friday, 3 January 2014