Skip to content

Instantly share code, notes, and snippets.

@throughnothing
Created April 22, 2011 04:37
Show Gist options
  • Save throughnothing/936021 to your computer and use it in GitHub Desktop.
Save throughnothing/936021 to your computer and use it in GitHub Desktop.
PRU examples -> Perl
# grep --- all lines including foo
ls -al | grep foo
ls -al | pru /foo/
ls -al | perl -ne 'print if /foo/'
perl -E 'say for <*foo*>'
# grep --- all lines including current date
ls -al | grep $(date +"%Y-%m-%d")
ls -al | pru 'include?(Time.now.strftime("%Y-%m-%d"))'
ls -al | perl -MTime::Piece -ne '$d=localtime->strftime("%Y-%m-%d");print if /$d/'
# grep --- all lines including foo but not self
ps -ef | grep foo | grep -v grep
ps -ef | pru 'include?("foo") and not include?("pru")'
ps -ef | perl -ne 'print if /foo/ && !/perl/'
# awk --- return second item
ls -al | awk '{print $2}'
ls -al | pru 'split(" ")[1]'
ls -al | perl -anE 'say $F[1]'
# awk --- count and average of all integers on second position
ls -al | awk '{ s += $2; } END {print "average" ,int(s/NR);print "count ",int(NR)}'
ls -al | pru 'split(" ")[1]' '"average #{mean(&:to_i)}\ncount #{size}"'
ls -al | perl -anE '$x+=$F[1]; END {say "average ".$x/$.."count $.")}'
ls -al | perl -anE '$x+=$F[1]; END {say qq(average @{[$x/$.]} count $.)}'
# wc --- count lines
ls -al | wc -l
ls -al | pru -r 'size'
ls -al | perl -nE 'END {say $.}'
# sed -- replace a 5 with five
ls -al | sed 's/5/five/'
ls -al | pru 'gsub(/5/,"five")'
ls -al | perl -pe 's/5/five/'
# every second line
ls -al | pru 'i % 2 == 0'
ls -al | perl -ne 'print unless $. % 2'
ls -al | perl -ne 'print if !($. % 2)'
# paste-friendly mime-types
curl https://github.com/mattetti/mimetype-fu/raw/master/lib/mime_types.yml | grep image | pru 'gsub(/(.*): (.*)/, %{"\\1" => "\\2",})'
curl https://github.com/mattetti/mimetype-fu/raw/master/lib/mime_types.yml | grep image | perl -pe 's/(.+): (.+)/$1 => $2/'
curl https://github.com/mattetti/mimetype-fu/raw/master/lib/mime_types.yml | perl -ne '/image/ && s/(.+): (.+)/$1 => $2/ && print'
# number of files by date:
ls -al | pru 'split(" ")[5]' 'grouped.map{|d, f| "#{d} : #{f.size}" }'
ls -al | perl -MData::Dump -anE '$x->{$F[4]}++; END {dd $x}'
# quotes inside a string
something | pru 'include?(%{"string"})'
something | perl -E 'say q("string")'
# vim:ft=conf
@ironcamel
Copy link

pru examples are from https://github.com/grosser/pru

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment