Skip to content

Instantly share code, notes, and snippets.

@bsnux
Last active November 13, 2019 18:05
Show Gist options
  • Save bsnux/cd73f9b5decdedd195e0dd41d0332794 to your computer and use it in GitHub Desktop.
Save bsnux/cd73f9b5decdedd195e0dd41d0332794 to your computer and use it in GitHub Desktop.
Useful Perl oneliners
#--
# The -p option takes each line of the standard input (STDIN) and assigns that line to the variable $_,
# then executes the script on that line. The $_ variable is a special variable which is set each
# iteration of the loop. When your script is complete it prints out the value of $_ to standard
# output (STDOUT). If there are command line arguments, -p will use those arguments as filenames and
# perform the same steps on each line of those files.
#
# The -n option is similar to -p except it does not print every line to the standard output.
#--
# Replace using carriage return
perl -p -e "s/apiVersion/---\napiVersion/g" a.yaml
# Replace in-place using carriage return
perl -p -i -e "s/apiVersion/---\napiVersion/g" a.yaml
# Simple search and replace
echo "test" | perl -p -e "s/t/T/g"
# Printing out file owner (similar to `awk`)
ls -l | perl -lane 'print $F[2]'
# Replace content in place and create a backup file with original content
perl -pi.bak -e 's/e/x/g' /tmp/test.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment