Thursday, 1 September 2016

Perl one liners

PERL ONE LINERS

This is a perl One lines blogs, helpful for all of you.These one liners help in almost all days of work .Weather you are working professional or not this can help you in leveraging your perl skill .Enjoy..!!!

Content-Type: text/x-zim-wiki
Wiki-Format: zim 0.4
Creation-Date: 2014-08-22T19:42:18+05:30

====== Perl one liner ======
Created Friday 22 August 2014

permanent change the contents of a file
__perl -pi -e 's/b/c/g' <filemame>__

to change on one particular line

__perl -pi -e 's/c/venkat/g if/3/' qwe__


to change whereever digits are mentioned 

__perl -pi -e 's/c/d/g if /\d/' qwe__

the sequence of p and i should be pi and not ip , in the option otherwise it will not work.

to find duplicate lines in a file 

 __perl -ne 'print if $a{$_}++' qwe __
__ls -lrt|awk '{print $1}'|perl -ne 'print if $a{$_}++'__

Numbering the output 

 __ls -lrt|perl -ne 'print " $.$_"'__


to print dulicate lines along with numbering 

__ls -lrt|awk '{print $9}'|perl -ne 'print if $a{$_}++'|perl -ne 'print "$. .$_"'__


to print all lines which are duplicate and also their corresponding position numbers 

__ls -lrt|awk '{print $9}'|perl -ne 'print "$. .$_" if $a{$_}++'__


finding the sum of the number of fields in a row 

__ls|perl -MList::Util=sum -alne 'print sum @F'__



finding the date certain days ago from current date 

__perl -MPOSIX -le '@t=localtime;$t[3]-=129999;print scalar localtime mktime @t'__
Thu Sep 19 08:56:24 1658


creating a 8 letter random password 
__perl -le 'print map{("a".."z")[rand 26]}1..8'__
akokhwae



decimal number associated with the IP address

__perl -le 'print unpack("N",127.0.0.0)'__
2130706432

to add the columns 

__ls -lrt|awk '{print $5}'|perl -lane '$sum+=$F[0];END{print $sum}'__
7212172

__ls -lrt|perl -lanF'\s+' -e '$sum+=$F[4];END{print $sum}'__
7212172


Double spacing a file 

__ls -lrt|perl -pe '$\="\n"'__


This is to substr the ls command 
 __ls -lrt|perl -ne '$v=substr($_,0,10);print "$v\n"'__


pattern matching 

__ls -lrt|perl -ne 'if(substr($_,0,10)=~/total|rw/){print "$_";}'__



perl -e 'print "venkat","is","a","good","boy\n"'
perl -we '$a="abac";$b="nanana";print "$a","  $b\n"'
clubbing during print using ()

perl -e '$a="abac";$b="nanana";print ("$a\n"),"  $b\n"'

printing tab

perl -e 'print "\t"'

printing an alarm
perl -e 'print "\a"'

backspace on one char

perl -e 'print "abc\b"'

printing binary to decimal
perl -e 'print 00100'

printing hex to dec
perl -e 'print 0xBDF'

here texts

efhjklx@6KV2ZR1:~$ perl -e 'print<<EOF;
> hello r u there
> EOF
> '
hello r u there


here text used for variable intepolation
efhjklx@6KV2ZR1:~$ perl -e '$a="adf";print<<'EOF';
> $a
> EOF'
adf


dot operator

perl -e 'print "a"."n\n"'

repeat operator
perl -e 'print "hi\n"x3'

range op

perl -e 'print 1..12,"\n"'


operators for numerics

efhjklx@6KV2ZR1:~$ perl -e '$a=6;$a+=3;print $a."\n"'
9
efhjklx@6KV2ZR1:~$ perl -e '$a=6;$a/=3;print $a."\n"'
2
efhjklx@6KV2ZR1:~$ 
efhjklx@6KV2ZR1:~$ 
efhjklx@6KV2ZR1:~$ perl -e '$a=6;$a**=3;print $a."\n"'
216
efhjklx@6KV2ZR1:~$ perl -e '$a=6;$a*=3;print $a."\n"'
18
efhjklx@6KV2ZR1:~$ perl -e '$a=6;$a-=3;print $a."\n"'
3
efhjklx@6KV2ZR1:~$ perl -e '$a=6;$a.=3;print $a."\n"'
63
efhjklx@6KV2ZR1:~$ perl -e '$a=6;$a%=3;print $a."\n"'
0
efhjklx@6KV2ZR1:~$ perl -e '$a=6;$a&=3;print $a."\n"'
2
efhjklx@6KV2ZR1:~$ perl -e '$a=6;$a|=3;print $a."\n"'
7
efhjklx@6KV2ZR1:~$ perl -e '$a=6;$a^=3;print $a."\n"'
5
efhjklx@6KV2ZR1:~$ perl -e '$a=6;$a<<=3;print $a."\n"'
48
efhjklx@6KV2ZR1:~$ perl -e '$a=6;$a>>=3;print $a."\n"'
0
efhjklx@6KV2ZR1:~$ perl -e '$a=6;$a&&=3;print $a."\n"'
3
efhjklx@6KV2ZR1:~$ perl -e '$a=6;$a||=3;print $a."\n"'
6


STDIN

perl -e '$a=<STDIN>;print $a."\n"'

chomping of STDIN

perl -e 'chomp($a=<STDIN>);print $a'

assigning arrays and getting them to print

perl -e '@a=`ls`;print @a[3...10]'

how to sleep in perl

perl -e '@a=`ls`;sleep 2;print @a[3...10];'

printing thru elements of array one by one

perl -e '@a=`ls`;for $a(@a){print $a;sleep 1}'

using scratch variable

perl -e '@a=`ls`;for(@a){print $_;sleep 1}'

use of reverse operator

perl -e '@a=`ls`;for(reverse(@a)){print $_;sleep 1}'

use of pop in arrays

perl -e '@a=`ls`;print pop(@a)'

use of sort op

perl -e '@a=`ls`;for(sort(@a)){print $_;sleep 1}'


use of @ARGV

perl -e 'foreach(@ARGV){print $_."\n"}' one 2 3

use of STDIN in while

perl -e 'while(<STDIN>){print $_}'

infinite loop

perl -e 'while(1){print "venkat"}'

how to split lines into rows

perl -e '@a=`ls -lrt`;foreach(@a){split/\s+/,$_;print $_[7]."\n"}'



printing

perl -e 'print "a"'

printing with warnings

perl -w -e 'print "a"'

printing with a new line by itself

perl -wl -e 'print "a"'

doing a cat with perl

perl -wl -e 'print "a"' abc.Z

here the n option does the while looping

perl -wnl -e 'print' abc.Z

here the p option does the while looping with automatic printing

perl -wpl -e '' abc.Z

to print sed like behaviour 

perl -wln -e 's/^/    /g;print;' abc.Z

the $. operator prints the line numbers

perl -wln -e 'print "$."' abc.Z

printf can be used to remove new line at the end of every feed

perl -wln -e 'printf "$."' corruptFiles.log


indentations for primary line in paragraph mode

perl -00 -wln -e 's/^/  /g;print' corruptFiles.log

perl -0777 -wln -e 's/^/  /g;print' corruptFiles.log


piping and $_ variable for printing

ls -lrt|perl -wnl -e 'print $_'


counting number of lines

ls -lrt|perl -wnl -e 'print $.'

ls -lrt|perl -wnl -e 'print $.'|wc -l


printing the result with period concatanation operator

ls -lrt|perl -wn -e 'print "$.".":"'
1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:34:35:36:37:38:39:40:41:42:43:44:45:46:47:48:49:50:51:52:


printing the result along with the result besides it

ls -lrt|perl -wn -e 'print "$.".": ";print'

ls -lrt|perl -wn -e 'print "$.|"."$_"'


printing the user defined variables

ls -lrt|perl -wn -e '$venkat="this is the line number:";print "$venkat"."$.:   "."$_"'


matching for $_

ls -lrt|perl -wlnaF'\s+' -e 'if(m/per/){print $F[7]}'



matching a string and running the if-else condition followed by END signifier

ls -lrt|perl -wlnaF'\s+' -e '$a="perl";if($a=~/abc/){print $F[7]}else{END{print "wrong"}}'


make a perl grep function

ls -lrt|perl -wnl -e '/perl/ and print'


printing with greping on coloum 

ls -lrt|perl -wlnaF'\s+' -e '$F[7]=~m/perl/ and print $F[7]'



escaping a single quote witin perl

perl -wln -e '/\047kj/ and print' abc.Zperl -wln -e '/^venkat/ and print' abc.Z


character class usage

perl -wln -e '/[\047]/ and print' abc.Z
perl -wln -e '/[\047]kj/ and print' abc.Z


usage of begining anchor

perl -wln -e '/^venkat/ and print' abc.Z


complemented characters

perl -wln -e '/[^venkat is good]/ and print' abc.Z

matching with dot

perl -wln -e '/k.e/ and print' abc.Z 


matching with character range

perl -wln -e '/[a-b]/ and print' abc.Z


matching with end anchor

perl -wln -e '/j$/ and print' abc.Z

getting the most recent of matches done

perl -wln -e '/j$/ and print $&' abc.Z


digit interpolation

perl -wln -e '/\bwin\d\d\d/ and print' abc.Z


to simulate grep -v 

perl -wln -e '/\d\d\d/ or print' abc.Z 


dicarding empty lines and printing with grep -v

perl -wln -e '/^$/ or print' abc.Z |perl -wnl -e '/\bwin\d\d\d/ or print'
perl -wln -e '/^$/ or /\bwin\d\d\d/ and print' abc.Z



printing the filename where the pattern isall_msisdn_sdp7 found

perl -wln -e '/\bwinCD\b/ and print $ARGV and close ARGV' *


ignoring case

perl -wnl -e '/wincd/i and print' abc.Z

egreping 

ls -lrt|perl -wnl -e '/abc|perl|personal/i and print'

combinatronics

ls -lrt|perl -wnl -e '/(abc|perl|personal)/i and print'


usnig quantifiers

ls -lrt|perl -wnl -e '/(rwxr)+/ and print'

repetition range

ls -lrt|perl -wnl -e '/(rwx){1,2}/ and print'

paragraph mode will print lines above and below the matched lines and only withing that paragraph

perl -00 -wnl -e '/kj/ and print' abc.Z 

how to print multiple args with commoa sepall_msisdnerated print

perl -wnl -e 'print "venkat ","is  perl -w -e 'print "what is the pattern you want to search:";chomp(my $a=<STDIN>);print "\n";@b=`ls -lrt`;foreach (@b){m/$a/i and print and print "\n"}'","at ","ericsson ";'
all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7alall_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7l_msisdn_sdp7
printing a tab and characters followed by it

perl -wl -e 'print "\ta\tb"'


converting from hexal to decimal

perl -wl -e 'print 0xF'
perl -wl -e 'print hex("0F")'


converting from octal to decimal--just prepend the number you want to print with 0

perl -wl -e 'print 01112'
perl -wl -e 'print oct("0F")'


heretext in perl

perl -wl -e 'print<<EOF
venkat is god 
and nice 
EOF
'


operators

perl -wl -e '4>=2 and print "corret"'



usnig quantifiers
repetition operator

perl -wl -e 'print "venkat\n"x4'


printing the ASCII value of a value

perl -wl -e 'print ord("a")'


cmp comparision this will give u 1 value if it is true otherwise empty

perl -wl -e 'print "venkat" cmp "ven"'
perl -wl -e 'print "venkat" ne "ven"'
1
perl -wl -e 'print "venkat" eq "ven"'



range operator

perl -wl -e 'print 1...10'


autoincrement and decrement operator

perl -wl -e '$a=1;print $a;$a++;print $a'


example of lexical variable with my operator

perl -wl -e '$a=10;print $a;{my $a=20;print $a}'

example with global tyesetting and local assignement

perl -wl -e 'our $a=10;print $a;{my $a=20;print $a};print $a'


greping in ls -lrt using STDIN,foreach and chomp

perl -wl -e 'print "hi";chomp(my $a=<STDIN>);@b=`ls -lrt`;foreach (@b){m/$a/i and print}'


perl -w -e 'print "what is the pattern you want to search:";chomp(my $a=<STDIN>);print "\n";@b=`ls -lrt`;foreach (@b){m/$a/i and print and print "\n"}'



assiging and printing an entire array or list

perl -wl -e '@b=(123,456);print @b'
perl -wl -e '@b=qw/123 456/;print @b'perl -l -e 'my $a=50;while($a>10){print $a;$a--}'



printing the element of an array with position number starting at 0

perl -wl -e 'print qw/123 456/[1]'
perl -wl -e 'print qw/123 456/[0,1]'

accessing ranges of elements in an array

perl -wl -e 'print qw/123 456 10 11 123 1123 125/[0..5]'

working with hashes

perl -l -e '@a=`ls -lrt`;%b=@a;for(keys(%b)){print values(%b)}'


perl -l -e '@a=qw/abc 123 def 456 ghi 789 jkl 101112/;%b=@a;for(keys(%b)){print "$_ is actually $b{$_}"}

defined fuctionality
all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7alall_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7all_msisdn_sdp7l_msisdn_sdp7
perl -l -e 'our $a;our $b,$b=20;if(defined $a){print "yes it is defined"}else{print "there is no value";if(defined $b){print $b}}'


usage of die 

perl -l -e 'our $a;our $b,$b=20;defined $a or die "there is no value";'

usage of for and shortcut to if conditional cascade block

perl -wl -e 'print "your choice";my $ch=<STDIN>;for($ch){$_==1 and print "One";$_==2 and print "two"}'

while looping 

perl -l -e 'my $a=50;while($a>10){print $a;$a--}'


infinite loop

perl -l -e 'while(1){print `date`}'


splitting columns

perl -wl -e '@a=`ls -lrt`;foreach $a(@a){@b=split/\s+/,$a;print $b[7]}'

opening a file and printing contents with file handle

perl -wl -e 'open FH,"/home/efhjklx/abc.Z" or die $!;@a=<FH>;print @a'perl -w -e '$|=1;for(1..20){print ".";sleep 1}'


perl -w -e 'open FH,"/home/efhjklx/abc.Z" or die $!;while(<FH>){print}'

appending to a file

perl -w -e 'open FH,">>/home/efhjklx/abc.Z" or die $!;print FH "venkat";print FH 1...10'

using select filehandle utility

perl -w -e 'open FH,">>/home/efhjklx/abc.Z" or die $!;select FH;print "venkat";print 1...10'


using $| for buffer switching off

perl -w -e '$|=1;for(1..20){print ".";sleep 1}'


reading from a directory

perl -wl -e 'opendir DH,"/home/efhjklx/Desktop";while($_=readdir(DH)){print}'

-c option is for checking the perl script syntax

perl -c new

to enter into CPAN shell for installation purpose

perl -MCPAN -e shell

example of installing module with aptitude command

first search for a module with following command

sudo apt-cache search perl*

then install using this command

sudo apt-get install libgeo-ipfree-perl


this another way of installing modules on perl

perl -MCPAN -e 'install modulename'

to update perl on your PC

sudo apt-get update && sudo apt-get install perl


how to parse fields with module Text::CSV_XS and delimiting a CSV file with pipe

perl -w -M'Text::CSV_XS' -e 'open FH,"/home/efhjklx/Desktop/a" or die $!;local $/="\n";while(<FH>){my $a=Text::CSV_XS->new;$a->parse(<FH>);my @f=$a->fields;local $"="|";print "@f\n"}'



to print time in perl in seconds format.

perl -e 'print time."\n"'


time elapsed between to variables 

perl -e 'my $a=time;sleep 5;my $b=time;print "the number of seconds elapsed is: ",$b-$a,"secs\n"'


printing localtime func in scalar notation

perl -e 'print scalar localtime,"\n"'
Fri Jul  6 03:58:02 2012


printing epoch time in local time scalar format

perl -e 'print scalar localtime(0),"\n"'
Thu Jan  1 05:30:00 1970



how to check which modules are installed on perl  

efhjklx@6KV2ZR1:~/Desktop$ instmodsh
Available commands are:
   l            - List all installed modules
   m <module>   - Select a module
   q            - Quit the program
cmd? l
Installed modules are:
   App::CSV
   DBD::Oracle
   Perl
   Spreadsheet::WriteExcel
cmd? q


this is where you get the module list for perl on ubuntu

http://packages.ubuntu.com/lucid/perl/


this is how to install .deb file for packages in ubuntu

sudo dpkg -i package.deb

to interact with databases

perl -we 'use DBI;my $dbh=DBI->connect("dbi:Oracle:orcl","venkatabc","venkat",{RaiseError=>1,AutoCommit=>0});my $sql=qq{select sysdate from dual};$sth=$dbh->prepare($sql);$sth->execute();while(my($abc)=$sth->fetchrow_array){print $abc,"\n";}$dbh->disconnect()'

getting all the output from DB

perl -we 'print "\n","Type the SQL command here:";our $a=<STDIN>;use DBI;my $dbh=DBI->connect("dbi:Oracle:orcl","venkatabc","venkat",{RaiseError=>1,AutoCommit=>0});my $sql=qq{$a};$sth=$dbh->prepare($sql);$sth->execute();while(my @abc=$sth->fetchrow_array){foreach $abc(@abc){$abc="\t" if !defined($abc);print "$abc\t";}print "\n";}$dbh->disconnect()'


perl -we 'use DBI;my $dbh=DBI->connect("dbi:Oracle:orcl","venkatabc","venkat",{RaiseError=>1,AutoCommit=>0});print $dbh->selectrow_array(qq{select * from employees});$dbh->disconnect()'


taking SQL statement as input from user

perl -we 'print "Type the SQL command here:";our $a=<STDIN>;use DBI;my $dbh=DBI->connect("dbi:Oracle:orcl","venkatabc","venkat",{RaiseError=>1,AutoCommit=>0});print $dbh->selectrow_array(qq{$a}),"\n";$dbh->disconnect()'



taking SQL command from user,taking field delimiter from user and representing data accordingly

perl -we 'print "\n","Type the SQL command here:";our $a=<STDIN>;print "Type the delimiter for field segregation:";our $b=<STDIN>;chomp($b);use DBI;my $dbh=DBI->connect("dbi:Oracle:orcl","venkatabc","venkat",{RaiseError=>1,AutoCommit=>0});my $sql=qq{$a};$sth=$dbh->prepare($sql);$sth->execute();while(my @abc=$sth->fetchrow_array){foreach $abc(@abc){$abc="\t" if !defined($abc);print "$abc",$b;}print "\n";}$dbh->disconnect()'



taking sql command and running it infinitely.also asking for quiting or not.

perl -we 'while(1){print "\n","Type the SQL command here:";our $a=<STDIN>;print "Type the delimiter for field segregation:";our $b=<STDIN>;chomp($b);use DBI;my $dbh=DBI->connect("dbi:Oracle:orcl","venkatabc","venkat",{RaiseError=>1,AutoCommit=>0});my $sql=qq{$a};$sth=$dbh->prepare($sql);$sth->execute();print "\n";while(my @abc=$sth->fetchrow_array){foreach $abc(@abc){$abc="\t" if !defined($abc);print "$abc",$b;}print "\n";}$dbh->disconnect();print "\n","Do you want to exit(y/n):";$n=<STDIN>;chomp($n);if($n eq "y"){exit}}'




opening pipes inside a perl program

perl -w -e 'open FH,"ls -lrt|cut -d\" \" -f 1|" or die $!;while(<FH>){print}'


file test conditions

perl -wl -e 'if(-e "/home/efhjklx/abc.Z"){print "hi"}'
findutils

globbing in file serching

perl -wl -e '$f=glob("abc*");print "$f"'


reading a directory

perl -wl -e 'opendir DH,"/home/efhjklx/" or die $!;while($_=readdir(DH)){print}'


defining a subroutine and using it

perl -wl -e 'abc();sub abc{opendir DH,"/home/efhjklx/" or die $!;while($_=readdir(DH)){print}}'


perl -wl -e '&abc;sub abc{opendir DH,"/home/efhjklx/" or die $!;while($_=readdir(DH)){print}}'



loading data into a hash from 2 files

perl -e '@a=`ls -lrt`;@n=`cat m`;foreach $n(@n){$m{$n}=@a[$n];}print values(%m)'


extracting from a hash values

perl -e 'print "\nThe key value:";chomp($d=<STDIN>);@a=`ls -lrt`;@m=`cat m`;foreach(@m){$c{$_}=@a[$_]};foreach(keys %c){if($_=~/$d/){print "\n",$c{$_},"\n"}}'



hash assignemnet and printing

perl -e '@a=`ls -lrt`;foreach $a(@a){@m=split/\s+/,$a;$c{$m[7]}=$m[8]}print $c{"12:25"}."\n"'


how to delete stobborn files 

perl -e '$dir="/var/opt/fds/CDR/scheduledJobCdrOut";opendir DH,$dir;foreach $file(readdir DH){if($file=~/e_/){unlink $file}}'