A Field Guide To The Perl Command Line. Andy Lester

Similar documents
IT441. Network Services Administration. Perl: File Handles

Perl. Interview Questions and Answers

print STDERR "This is a debugging message.\n";

*roff code is suitable for display on a terminal using nroff(1), normally via man(1), or printing using troff(1).

More Scripting and Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

This is a list of questions and answers about Unicode in Perl, intended to be read after perlunitut.

sottotitolo A.A. 2016/17 Federico Reghenzani, Alessandro Barenghi

Pathologically Eclectic Rubbish Lister

A shell can be used in one of two ways:

Automated Testing of Large Projects With Perl. Andy Lester

Essential Linux Shell Commands

Introduc)on to Unix and Perl programming

Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Lecture 3 Tonight we dine in shell. Hands-On Unix System Administration DeCal

(Refer Slide Time: 01:12)

1. Introduction. 2. Scalar Data

Should you know scanf and printf?

Regular expressions and case insensitivity

Basics. I think that the later is better.

Introduc)on to Unix and Perl programming

Useful Perl idioms. What this chapter covers:

# or you can even do this if your shell supports your native encoding

A Crash Course in Perl5

bash, part 3 Chris GauthierDickey

CSE 390a Lecture 2. Exploring Shell Commands, Streams, and Redirection

Perl one-liners. Rev Copyright 2006 Bartosz Filipowicz

Programming introduction part I:

Indian Institute of Technology Kharagpur. PERL Part II. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. I.I.T.

Learning Perl 6. brian d foy, Version 0.6, Nordic Perl Workshop 2007

My Favorite bash Tips and Tricks

Introduction to Perl. c Sanjiv K. Bhatia. Department of Mathematics & Computer Science University of Missouri St. Louis St.

Classnote for COMS6100

NAME SYNOPSIS. Perl version documentation - Pod::Parser. Pod::Parser - base class for creating POD filters and translators.

Be sure to read ENCODING if your Pod contains non-ascii characters.

Programming Perls* Objective: To introduce students to the perl language.

Perl. Many of these conflict with design principles of languages for teaching.

When talking about how to launch commands and other things that is to be typed into the terminal, the following syntax is used:

Regular Expressions 1

I/O and Shell Scripting

Perl. Perl. Perl. Which Perl

Outline. CS3157: Advanced Programming. Feedback from last class. Last plug

The perlfaq is an evolving document. Read the latest version at It is also included in the standard Perl distribution.

Introduction to Unix

use Pod::Man; my $parser = Pod::Man->new (release => $VERSION, section => 8);

Week Overview. Simple filter commands: head, tail, cut, sort, tr, wc grep utility stdin, stdout, stderr Redirection and piping /dev/null file

Using UNIX. -rwxr--r-- 1 root sys Sep 5 14:15 good_program

What is PERL?

Last Time. on the website

do shell script in AppleScript

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

Fortunately, the layout is much more legible, more like BASIC's PRINT USING statement. Think of it as a poor man's nroff(1). nroff

Command Interpreters. command-line (e.g. Unix shell) On Unix/Linux, bash has become defacto standard shell.

subtest $builder->subtest($name,

... and run the script as /path/to/script.pl. Of course, it'll need to be executable first, so chmod 755 script.pl (under Unix).

NAME SYNOPSIS. Perl version documentation - Pod::Parser. Pod::Parser - base class for creating POD filters and translators.

Welcome! COMP s1. Programming Fundamentals

HEP Computing Part II Scripting Marcella Bona

Introduction to Perl. Perl Background. Sept 24, 2007 Class Meeting 6

Vi & Shell Scripting

file:///users/cerdmann/downloads/ harvard-latest.html

CSCI 4152/6509 Natural Language Processing. Perl Tutorial CSCI 4152/6509. CSCI 4152/6509, Perl Tutorial 1

Documenting Code. Plain Old Documentation (POD) markup language

A lot of people make repeated mistakes of not calling their functions and getting errors. Make sure you're calling your functions.

Indian Institute of Technology Kharagpur. PERL Part III. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. I.I.T.

CSE 390a Lecture 2. Exploring Shell Commands, Streams, Redirection, and Processes

This page covers the very basics of understanding, creating and using regular expressions ('regexes') in Perl.

A control expression must evaluate to a value that can be interpreted as true or false.

The following content is provided under a Creative Commons license. Your support

COMS 3101 Programming Languages: Perl. Lecture 2

Practical Perl Tools. From the Editor. Reflowing and Reformatting Text

Introduction Variables Helper commands Control Flow Constructs Basic Plumbing. Bash Scripting. Alessandro Barenghi

Bash Check If Command Line Parameter Exists

Python Working with files. May 4, 2017

Besides your local system, you can find the perlfaq on the web, including at

MITOCW watch?v=rvrkt-jxvko

Lesson 4: Who Goes There?

STATS Data Analysis using Python. Lecture 15: Advanced Command Line

AHHHHHHH!!!! NOT TESTING! Anything but testing! Beat me, whip me, send me to Detroit, but don t make me write tests!

Procedures, Parameters, Values and Variables. Steven R. Bagley

Here's an example of how the method works on the string "My text" with a start value of 3 and a length value of 2:

CS 105 Perl: Completing the Toolbox

NAME DESCRIPTION. Declarations. Perl version documentation - perlsyn. perlsyn - Perl syntax

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Subroutines. Subroutines. The Basics. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc.

PERL. Pattern Extraction and Reporting Language. Example: $x = 10 $value = $x + 1 $word = "hello"

Files

Bioinformatics. Computational Methods II: Sequence Analysis with Perl. George Bell WIBR Biocomputing Group

CMSC 331 Final Exam Fall 2013

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #47. File Handling

Linux II and III. Douglas Scofield. Crea-ng directories and files 18/01/14. Evolu5onary Biology Centre, Uppsala University

18-Sep CSCI 2132 Software Development Lecture 6: Links and Inodes. Faculty of Computer Science, Dalhousie University. Lecture 6 p.

Scripting Languages Perl Basics. Course: Hebrew University

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

Processes and Shells

Pod::Usage, pod2usage() - print a usage message from embedded pod documentation

Lec 1 add-on: Linux Intro

# use a BEGIN block so we print our plan before MyModule is loaded BEGIN { plan tests => 14, todo => [3,4] }

Introduction to UNIX. Introduction. Processes. ps command. The File System. Directory Structure. UNIX is an operating system (OS).

Introduction to UNIX. CSE 2031 Fall November 5, 2012

Transcription:

A Field Guide To The Perl Command Line Andy Lester andy@petdance.com http://petdance.com/perl/

Where we're going Command-line == super lazy The magic filehandle The -e switch -p, -n: Implicit looping -a, -F: Field handling -i: Editing in place -l, -0: Record separator handling

-e: Your program The -e is your program. It's repeatable. Mind the quoting Mind the semicolons Anything can go in here, even BEGIN blocks.

-e examples $ perl -e'print "Hello, World!\n"' Hello, World! # Perl as your calculator $ perl -e'print 1024*1024/80, "\n"' 13107.2 # Mind the quotes: WRONG $ perl -MCGI -e"print $CGI::VERSION" # print ::VERSION # Better & best $ perl -MCGI -e"print \$CGI::VERSION" $ perl -MCGI -e'print $CGI::VERSION' # Windows C:\> perl -e"print \"Hello, World!\n\""

The magic filehandle Perl does lots of the common stuff for you Diamond operator takes STDIN or file input from @ARGV files Modify your @ARGV before reading Do command-line option parsing Modify @ARGV on your own Currently-read filename is in $ARGV

Magic filehandle for my $file ( @ARGV ) { open( my $fh, $file ) or die "Can't open $file: $!\n"; while ( my $line = <$fh> ) { # do something with $line } close $fh; } $ perl myprog.pl file1.txt file2.txt file3.txt # Instead, do this: while ( my $line = <> ) { # do something } # Also automatically works with redirection $ grep blah blah blah perl myprog.pl

-p & -n: Implicit looping # -n wraps your code in this loop (basically) while (<>) { # Your code goes here } # -p wraps your code in this loop (basically) while (<>) { # Your code goes here } print;

# Program to print output with line numbers # (in case cat -n doesn't do it for ya) while (<>) { $_ = sprintf( "%05d: %s", $., $_ ); print; # implicitly print $_ } # Try this instead #!/usr/bin/perl -p -p examples $_ = sprintf( "%05d: %s", $., $_ ); # or even shorter as: $ perl -p -e'$_ = sprintf( "%05d: %s", $., $_ )'

-n examples # Print commented lines $ perl -n -e'print if /^\s*#/' # Print values that look like dollars, like "$43.50" #!/usr/bin/perl -n while ( /\$(\d+\.\d\d)/g ) { print $1, "\n"; } # Or total 'em up #!/usr/bin/perl -n BEGIN { $total=0 } END { printf( "%.2f\n", $total ) } while ( /\$(\d+\.\d\d)/g ) { $total += $1; }

-l: line-ending handling Automatically adds or removes '\n' In effect: chomp()s everything on input Adds '\n' to each print A godsend for one-liners

-0: Input record sep # That's hyphen-zero, not hyphen-oh. # Lets you specify $/ from the command line. # Value is in octal. # You could use -e'begin { $/="whatever"}' # Work on a Mac file with chr(13) as the separator perl -015 -e... # Special values: -00 (zero zero) = paragraph mode (same as $/="") -0777 = slurp mode (same as $/=undef) # Print out all non-literal POD code: $ perl -n -00 -e'print unless /^\s+/;' article.pod

-i: edit in place Opens each file, reads from it, and replaces it with STDOUT. Avoids the "make a foo file" dance Can specify a backup file like -i.bak Old file foo.txt becomes foo.txt.bak

-a and -F: Autosplitting -a makes Perl split $_ into @F on whitespace Implicitly turns @F into a list of fields -F specifies what to split on if not whitespace

-a and -F examples # Print total of 10th column from an Apache log # (total number of bytes transferred) $ perl -l -a -n -e'$n+=$f[9];end{print $n}' access_log # Print all users that have a login shell $ perl -l -n -a -F: \ -e'print $F[0] unless $F[-1] eq "/bin/false"' \ /etc/passwd # Note that even though there are no slashes, # -F: still means that the split regex is /:/

Option stacking You can combine options on the command line, if they're not ambiguous. $ perl -l -n -a -F: -e'...' $ perl -lnaf: -e'...' But don't do it. It adds complexity and potential bugs. $ perl -p -i -l -e'$_=substr($_,0,40)' myfile.txt $ perl -pil -e'$_=substr($_,0,40)' myfile.txt What you think is -l is actually telling -i to append "l" to the backup file.

-m & -M: Module loading -mfoo does a "use Foo();" -MFoo does a "use Foo;" -M-Foo does a "no Foo;" Doesn't import any symbols Imports any default symbols. But who uses "no" anyway?

-m/-m examples # What version of CGI do I have? $ perl -MCGI -le'print $CGI::VERSION' 2.89 # Some modules are meant for the command line $ perl -MCPAN -e'install "Module::Name"' # Text::Autoformat exports autoformat() by default $ perl -MText::Autoformat -e'autoformat'

Perl respects command-line options on the #!perl line $ perl -i -pe's/foo/bar/g' #!/usr/bin/perl -i -p s/foo/bar/g; Wrapping up This works on Windows, even though Windows doesn't use the shebang line itself. One-liner to convert Mac files: $ perl -i.bak -l015 -pe1 *.txt