From b2d6885850b7970d92f3c9b24e3c5050f3fcfa57 Mon Sep 17 00:00:00 2001 From: trimstray Date: Tue, 26 Nov 2019 08:10:04 +0100 Subject: [PATCH] add perl one-liners - signed-off-by: trimstray --- README.md | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/README.md b/README.md index 12ffb2b..24e1757 100644 --- a/README.md +++ b/README.md @@ -1476,6 +1476,7 @@ In Firefox's addressbar, you can limit results by typing special characters befo * [awk](#tool-awk) * [sed](#tool-sed) * [grep](#tool-grep) + * [perl](#tool-perl) ##### Tool: [terminal](https://en.wikipedia.org/wiki/Linux_console) @@ -3831,6 +3832,62 @@ grep -- -- filename grep "\-\-" filename ``` +##### Tool: [perl](https://www.perl.org/) + +###### Search and replace (in place) + +```bash +perl -i -pe's/SEARCH/REPLACE/' filename +``` + +###### Edit of `*.conf` files changing all foo to bar (and backup original) + +```bash +perl -p -i.orig -e 's/\bfoo\b/bar/g' *.conf +``` + +###### Prints the first 20 lines from `*.conf` files + +```bash +perl -pe 'exit if $. > 20' *.conf +``` + +###### Search lines 10 to 20 + +```bash +perl -ne 'print if 10 .. 20' filename +``` + +###### Delete first 10 lines (and backup original) + +```bash +perl -i.orig -ne 'print unless 1 .. 10' filename +``` + +###### Delete all but lines between foo and bar (and backup original) + +```bash +perl -i.orig -ne 'print unless /^foo$/ .. /^bar$/' filename +``` + +###### Reduce multiple blank lines to a single line + +```bash +perl -p -i -00pe0 filename +``` + +###### Convert tabs to spaces (1t = 2sp) + +```bash +perl -p -i -e 's/\t/ /g' filename +``` + +###### Read input from a file and report number of lines and characters + +```bash +perl -lne '$i++; $in += length($_); END { print "$i lines, $in characters"; }' filename +``` + #### Shell functions  [[TOC]](#anger-table-of-contents) ##### Table of Contents