Friday, November 27, 2015

Language: Perl (One-liners).....

Perl on-liners can perform a cariety od useful tasks,. Some interesting ones..
Codes tested on bash shell..
-e :For execution; -p: For printing; -i: Edited in place
-------------------------------------------------------------------------
#print lines. pattern, reverse lines, reverse alphabets, shuffle lines, alphabet case change, space manipulation, pattern substitution, column manipulation, blank line manipulation, line numbering, calculations, time printing, word manipulation.........

# Print the first line of a file (head -1)
perl -ne 'print; exit'

# Print the first 10 lines of a file (head -10)
perl -ne 'print if $. <= 10'
perl -ne '$. <= 10 && print'
perl -ne 'print if 1..10'

# Print the last line of a file (tail -1)
perl -ne '$last = $_; END { print $last }'
perl -ne 'print if eof'

# Print the last 10 lines of a file (tail -10)
perl -ne 'push @a, $_; @a = @a[@a-10..$#a]; END { print @a }'

# Print only lines that match a regular expression
perl -ne '/regex/ && print'

# Print only lines that do not match a regular expression
perl -ne '!/regex/ && print'

# Print the line before a line that matches a regular expression

perl -ne '/regex/ && $last && print $last; $last = $_'

#Print  the next line following this pattern
perl -ne 'if ($p) { print; $p = 0 } $p++ if /pattern/' file
# Print lines that match regex AAA and regex BBB in any order
perl -ne '/AAA/ && /BBB/ && print'
# Print lines that don't match match regexes AAA and BBB
perl -ne '!/AAA/ && !/BBB/ && print'
# Print lines that match regex AAA followed by regex BBB followed by CCC
perl -ne '/AAA.*BBB.*CCC/ && print'
# Print lines that are 80 chars or longer
perl -ne 'print if length >= 80'
# Print lines that are less than 80 chars in length
perl -ne 'print if length < 80'
# Print only line 16
perl -ne '$. == 16 && print && exit'
# Print all lines except line 24
perl -ne '$. != 24 && print'
perl -ne 'print if $. != 24'
# Print only lines 13, 19 and 67
perl -ne 'print if $. == 13 || $. == 19 || $. == 67'
perl -ne 'print if int($.) ~~ (13, 19, 67)' 
# Print all lines between two regexes (including lines that match regex)
perl -ne 'print if /regex1/../regex2/'
# Print all lines from line 17 to line 30
perl -ne 'print if $. >= 17 && $. <= 30'
perl -ne 'print if int($.) ~~ (17..30)'
perl -ne 'print if grep { $_ == $. } 17..30'
# Print the longest line
perl -ne '$l = $_ if length($_) > length($l); END { print $l }'
# Print the shortest line
perl -ne '$s = $_ if $. == 1; $s = $_ if length($_) < length($s); END { print $s }'
# Print all lines that contain a number
perl -ne 'print if /\d/'
# Find all lines that contain only a number
perl -ne 'print if /^\d+$/'
# Print all lines that contain only characters
perl -ne 'print if /^[[:alpha:]]+$/
# Print every second line
perl -ne 'print if $. % 2'
# Print every second line, starting the second line
perl -ne 'print if $. % 2 == 0'
# Print all lines that repeat (find duplicate lines)
perl -ne 'print if ++$a{$_} == 2'
# Print all unique lines (find unique lines)
perl -ne 'print unless $a{$_}++'
# Print the first field (word) of every line (like cut -f 1 -d ' ')
perl -alne 'print $F[0]'
#Print paragraphs in reverse order (there must be space between lines).
 perl -00 -e 'print reverse <>' file

#Print all lines in reverse order (what appears is gibberish, may be good for secret signals)
perl -lne 'print scalar reverse $_'  file
perl -lne 'print scalar reverse'  file

#Shuffle rows in a data file
cat file | perl -MList::Util=shuffle -e 'print shuffle(<STDIN>);\n'
#!/usr/bin/perl
use List::Util 'shuffle';
@list = <STDIN>;
print shuffle(@list);

#Convert first letter of each line into uppercase 
perl -nle 'print ucfirst lc' file
perl -ple 's/(\w+)/\u$1/g' file

#Change the alphabet case
perl -ple 'y/A-Za-z/a-zA-Z/' file

# Convert all text to uppercase
perl -nle 'print uc'
perl -ple '$_=uc'
perl -nle 'print "\U$_"'

# Convert all text to lowercase
perl -nle 'print lc'
perl -ple '$_=lc'
perl -nle 'print "\L$_"'

# Uppercase only the first word of each line
perl -nle 'print ucfirst lc'

perl -nle 'print "\u\L$_"'

#Remove leading white spaces 
perl -ple 's/^[ \t]+//' file
perl -ple 's/^\s+//' file

#Remove trailing white spaces (its not much visible though, so skipping result)
perl -ple 's/[ \t]+$//' file
perl -ple 's/\s+$//' file

#Remove whitespace (spaces, tabs) from the beginning an end  
perl -ple 's/^[ \t]+|[ \t]+$//g' file
perl -ple 's/^\s+|\s+$//g' file

# Double space a file, except the blank lines
perl -pe '$_ .= "\n" unless /^$/' file

#Double space a file
perl -pe '$\="\n"' file
perl -pe 'BEGIN { $\="\n" }' file
perl -pe '$_ .= "\n"' file
perl -pe 's/$/\n/' file
perl -nE 'say' file
perl -pe '$_ .= "\n" if /\S/' file

# Triple space a file
perl -pe '$\="\n\n"' file
perl -pe '$_.="\n\n"' file

# N-space a file
perl -pe '$_.="\n"x7' file

#Substitute pattern1 (xyz here) with pattern2 (abc here) on  each line
perl -pe 's/xyz/abc/' file

#Substitute pattern1 with pattern2 on  lines matching pattern3 (lmn here)
perl -pe '/lmn/ && s/xyz/abc/' file
perl -pe 's/xyz/abc/ if /lmn/' file

#Shuffle all fields on a line
perl -MList::Util=shuffle -alne 'print "@{[shuffle @F]}"' file
perl -MList::Util=shuffle -alne 'print join " ", shuffle @F' file

#Take a multi column file and convert into uni column . Suppose
1   9
2   3
5   6
8   4
xargs -n1 < multicolumn
1                                                                                                   
9                                                                                                   
2                                                                                                   
3                                                                                                   
5                                                                                                   
6                                                                                                   
8                                                                                                   
4

#Add a blank line before every line
perl -pe 's//\n/' file

#Remove all blank lines
perl -ne 'print unless /^$/' file
perl -lne 'print if length' file
perl -ne 'print if /\S/' file

# Remove all consecutive blank lines, leaving just one
perl -00 -pe '' file
perl -00pe0 file

#Number all lines in a file
perl -pe '$_ = "$. $_"' file

#Number only non-empty lines in a file
perl -pe '$_ = ++$a." $_" if /./' file

#Number and print only non-empty lines in a file (drop empty lines)
perl -ne 'print ++$a." $_" if /./' file

#Number all lines but print line numbers only non-empty lines
perl -pe '$_ = "$. $_" if /./' file

#Number only lines that match a pattern, print others unmodified
perl -pe '$_ = ++$a." $_" if /regex/' file

#Number and print only lines that match a pattern
perl -ne 'print ++$a." $_" if /regex/'

#Number all lines, but print line numbers only for lines that match a pattern
perl -pe '$_ = "$. $_" if /regex/'

#Number all lines in a file using a custom format
perl -ne 'printf "%-5d %s", $., $_' file

#Print the total number of lines in a file (like wc -l)
perl -lne 'END { print $. }' file
perl -le 'print $n=()=<>' file
perl -le 'print scalar(()=<>)' file
perl -le 'print scalar(@foo=<>)' file
perl -ne '}{print $.' file
perl -nE '}{say $.' file

# Print the number of non-empty lines in a file
perl -le 'print scalar(grep{/./}<>)' file
perl -le 'print ~~grep{/./}<>' file
perl -le 'print~~grep/./,<>' file
perl -E 'say~~grep/./,<>' file

# Print the number of empty lines in a file
perl -lne '$a++ if /^$/; END {print $a+0}' file
perl -le 'print scalar(grep{/^$/}<>)' file
perl -le 'print ~~grep{/^$/}<>' file
perl -E 'say~~grep{/^$/}<>' file

# Print the number of lines in a file that match a pattern (grep -c)
perl -lne '$a++ if /regex/; END {print $a+0}' file
perl -nE '$a++ if /regex/; END {say $a+0}' file

# Check if a number is a prime
perl -lne '(1x$_) !~ /^1?$|^(11+?)\1+$/ && print "$_ is prime"'

# Print the sum of all the fields on a line
perl -MList::Util=sum -alne 'print sum @F'

# Print the sum of all the fields on all lines
perl -MList::Util=sum -alne 'push @S,@F; END { print sum @S }'
perl -MList::Util=sum -alne '$s += sum @F; END { print $s }'
# Find the minimum element on a line
perl -MList::Util=min -alne 'print min @F'

# Find the minimum element over all the lines
perl -MList::Util=min -alne '@M = (@M, @F); END { print min @M }'
perl -MList::Util=min -alne '$min = min @F; $rmin = $min unless defined $rmin && $min > $rmin; END { print $rmin }'

# Find the maximum element on a line
perl -MList::Util=max -alne 'print max @F'

# Find the maximum element over all the lines
perl -MList::Util=max -alne '@M = (@M, @F); END { print max @M }'

# Replace each field with its absolute value
perl -alne 'print "@{[map { abs } @F]}"'

# Find the total number of fields (words) on each line
perl -alne 'print scalar @F'

# Print the total number of fields (words) on each line followed by the line
perl -alne 'print scalar @F, " $_"'

# Find the total number of fields (words) on all lines
perl -alne '$t += @F; END { print $t}'

# Print the total number of fields that match a pattern
perl -alne 'map { /regex/ && $t++ } @F; END { print $t }'
perl -alne '$t += /regex/ for @F; END { print $t }'
perl -alne '$t += grep /regex/, @F; END { print $t }'

# Print the total number of lines that match a pattern
perl -lne '/regex/ && $t++; END { print $t }'

# Print the number PI to n decimal places
perl -Mbignum=bpi -le 'print bpi(n)'

# Print the number PI to 39 decimal places
perl -Mbignum=PI -le 'print PI'

# Print the number E to n decimal places
perl -Mbignum=bexp -le 'print bexp(1,n+1)'

# Print the number E to 39 decimal places
perl -Mbignum=e -le 'print e'

# Calculate factorial of 5
perl -MMath::BigInt -le 'print Math::BigInt->new(5)->bfac()'
perl -le '$f = 1; $f *= $_ for 1..5; print $f'

# Calculate greatest common divisor (GCM)
perl -MMath::BigInt=bgcd -le 'print bgcd(@list_of_numbers)'

# Calculate GCM of numbers 20 and 35 using Euclid's algorithm
perl -le '$n = 20; $m = 35; ($m,$n) = ($n,$m%$n) while $n; print $m'

# Calculate least common multiple (LCM) of numbers 35, 20 and 8
perl -MMath::BigInt=blcm -le 'print blcm(35,20,8)'

# Calculate LCM of 20 and 35 using Euclid's formula: n*m/gcd(n,m)
perl -le '$a = $n = 20; $b = $m = 35; ($m,$n) = ($n,$m%$n) while $n; print $a*$b/$m'

# Generate 10 random numbers between 5 and 15 (excluding 15)
perl -le '$n=10; $min=5; $max=15; $, = " "; print map { int(rand($max-$min))+$min } 1..$n'

# Find and print all permutations of a list
perl -MAlgorithm::Permute -le '$l = [1,2,3,4,5]; $p = Algorithm::Permute->new($l); print @r while @r = $p->next'

# Generate the power set
perl -MList::PowerSet=powerset -le '@l = (1,2,3,4,5); for (@{powerset(@l)}) { print "@$_" }'
# Convert an IP address to unsigned integer
perl -le '$i=3; $u += ($_<<8*$i--) for "127.0.0.1" =~ /(\d+)/g; print $u'
perl -le '$ip="127.0.0.1"; $ip =~ s/(\d+)\.?/sprintf("%02x", $1)/ge; print hex($ip)'
perl -le 'print unpack("N", 127.0.0.1)'
perl -MSocket -le 'print unpack("N", inet_aton("127.0.0.1"))'

# Generate and print the alphabet
perl -le 'print a..z'
perl -le 'print ("a".."z")'
perl -le '$, = ","; print ("a".."z")'
perl -le 'print join ",", ("a".."z")'

# Generate and print all the strings from "a" to "zz"
perl -le 'print ("a".."zz")'
perl -le 'print "aa".."zz"'

# Create a hex lookup table
@hex = (0..9, "a".."f")

# Convert a decimal number to hex using @hex lookup table
perl -le '$num = 255; @hex = (0..9, "a".."f"); while ($num) { $s = $hex[($num%16)&15].$s; $num = int $num/16 } print $s'
perl -le '$hex = sprintf("%x", 255); print $hex'
perl -le '$num = "ff"; print hex $num'

# Generate a random 8 character password
perl -le 'print map { ("a".."z")[rand 26] } 1..8'
perl -le 'print map { ("a".."z", 0..9)[rand 36] } 1..8'

# Create a string of specific length
perl -le 'print "a"x50'

# Create a repeated list of elements
perl -le '@list = (1,2)x20; print "@list"'

# Create an array from a string
@months = split ' ', "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
@months = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/

# Create a string from an array
@stuff = ("hello", 0..9, "world"); $string = join '-', @stuff

# Find the numeric values for characters in the string
perl -le 'print join ", ", map { ord } split //, "hello world"'

# Convert a list of numeric ASCII values into a string
perl -le '@ascii = (99, 111, 100, 105, 110, 103); print pack("C*", @ascii)'
perl -le '@ascii = (99, 111, 100, 105, 110, 103); print map { chr } @ascii'

# Generate an array with odd numbers from 1 to 100
perl -le '@odd = grep {$_ % 2 == 1} 1..100; print "@odd"'
perl -le '@odd = grep { $_ & 1 } 1..100; print "@odd"'

# Generate an array with even numbers from 1 to 100
perl -le '@even = grep {$_ % 2 == 0} 1..100; print "@even"'

# Find the length of the string
perl -le 'print length "one-liners are great"'

# Find the number of elements in an array
perl -le '@array = ("a".."z"); print scalar @array'
perl -le '@array = ("a".."z"); print $#array + 1'

# Convert an unsigned integer to an IP address
perl -MSocket -le 'print inet_ntoa(pack("N", 2130706433))'
perl -le '$ip = 2130706433; print join ".", map { (($ip>>8*($_))&0xFF) } reverse 0..3'
perl -le '$ip = 2130706433; $, = "."; print map { (($ip>>8*($_))&0xFF) } reverse 0..3'
# Print UNIX time (seconds since Jan 1, 1970, 00:00:00 UTC)
perl -le 'print time'

# Print GMT (Greenwich Mean Time) and local computer time
perl -le 'print scalar gmtime'
perl -le 'print scalar localtime'

# Print local computer time in H:M:S format
perl -le 'print join ":", (localtime)[2,1,0]'

# Print yesterday's date
perl -MPOSIX -le '@now = localtime; $now[3] -= 1; print scalar localtime mktime @now'

# Print date 14 months, 9 days and 7 seconds ago
perl -MPOSIX -le '@now = localtime; $now[0] -= 7; $now[4] -= 14; $now[7] -= 9; print scalar localtime mktime @now'

# Prepend timestamps to stdout (GMT, localtime)
tail -f logfile | perl -ne 'print scalar gmtime," ",$_'
tail -f logfile | perl -ne 'print scalar localtime," ",$_'

No comments:

Post a Comment