Perl one-liners
suggest changeSome tasks can be conveniently achieved with Perl one-liners. Perl is a scripting language originating in the environment of another operating system. Since many Windows computing environments have Perl installed, Perl one-liners are a natural and compact extension of Windows batch scripting.
Examples:
echo "abcbbc"| perl -pe "s/a.*?c/ac/"
- Lets Perl act as sed, the utility that supports textual replacements specified using regular expressions.
echo a b| perl -lane "print $F[1]"
- Lets Perl act as cut command, displaying the 2nd field or column of the line, in this case b. Use
$F[2]
to display 3rd field; indexing starts at zero. Native solution:FOR
/f
.
- Lets Perl act as cut command, displaying the 2nd field or column of the line, in this case b. Use
perl -ne "print if /\x22hello\x22/" file.txt
perl -ne "$. <= 10 and print" MyFile.txt
- Lets Perl act as head -10 command, outputting the first 10 lines of the file.
perl -e "sleep 5"
- Waits for 5 seconds.
for /f %i in ('perl -MPOSIX -le "print strftime '%Y-%m-%d', localtime"') do @set isodate=%i
- Gets current date in the ISO format into
isodate
variable.
- Gets current date in the ISO format into
perl -MWin32::Clipboard -e "print Win32::Clipboard->Get()"
- Outputs the text content of the clipboard. When stored to getclip.bat, yields a handy getclip command to complement CLIP command.
perl -MText::Diff -e "print diff 'File1.txt', 'File2.txt'"
- Outputs differences between two files in a format similar to diff command known from other operating systems, including context lines, lines starting with + and lines starting with -.
On the web, Perl one-liners are often posted in the command-line conventions of another operating system, including the use of apostrophe ('
) to surround the arguments instead of Windows quotation marks. These need to be tweaked for Windows.
Links:
- Perl One-Liners by Peteris Krumins at github.com
- Why doesn't my Perl one-liner work on Windows? at stackoverflow.com
- W:One-liner program#Perl
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents