Slide 1

Slide 1 text

Command-line Filters: Time to Shine
 The Perl and Raku Conference
 
 2023-07-12

Slide 2

Slide 2 text

Command-line Filters: Time to Shine PLEASE DOWNLOAD THESE SLIDES! 
 http://speakerdeck.com/util
 <<< >>> >>> <<<

Slide 3

Slide 3 text

/me
 
 Bruce Gray


Slide 4

Slide 4 text

/me
 
 Bruce Gray
 
 'Util'

Slide 5

Slide 5 text

Command-line Filters: Time to Shine

Slide 6

Slide 6 text

Order of Battle • • • • •

Slide 7

Slide 7 text

Order of Battle • What are fi lters? • • • •

Slide 8

Slide 8 text

Order of Battle • What are fi lters? • How to use fi lters? < | > >> • • •

Slide 9

Slide 9 text

Order of Battle • What are fi lters? • How to use fi lters? < | > >> • Really? • •

Slide 10

Slide 10 text

Order of Battle • What are fi lters? • How to use fi lters? < | > >> • Really! • •

Slide 11

Slide 11 text

Order of Battle • What are fi lters? • How to use fi lters? < | > >> • Really! • HTF? •

Slide 12

Slide 12 text

Order of Battle • What are fi lters? • How to use fi lters? < | > >> • Really! • HTF? • Now You!

Slide 13

Slide 13 text

Rationale • 
 • 
 •

Slide 14

Slide 14 text

Rationale • PyCharm IDE: Python
 • 
 •

Slide 15

Slide 15 text

Rationale • PyCharm IDE: Python
 • RStudio: R-Lang
 •

Slide 16

Slide 16 text

Rationale • PyCharm IDE: Python
 • RStudio: R-Lang
 • Jupyter Notebook (JupyterLab?): Everything

Slide 17

Slide 17 text

2023-07-12

Slide 18

Slide 18 text

Tedious,
 and Well Covered Elsewhere • Getting a command line
 (Terminal, CMD, xterm...) • Current directory
 (and Windows "Drive Letter") • Text Editor
 (not Word Processor!) • wsl --install

Slide 19

Slide 19 text

Very Useful,
 but Not Covered This Year • Command-line Perl • -e and -E • -n and -p • -a and -F • -l and -0777 • BEGIN and END

Slide 20

Slide 20 text

perl -0777 -wnE 's{\rMSH}{\nMSH}g; s{\r\z}{\n}; my @L = split "\n";
 s{\A.+\r(OBR)}{$1} for @L; s{\r.+\z}{} for @L; tr{|}{\t} for @L; for (@L) {my @F = split "\t"; $ARGV =~ /lab_(\d\d\d\d)-/ or warn; say "$1\t$F[20]"}' lab_*.txt > ../assessions.txt

Slide 21

Slide 21 text

Redirect, Pipe • seq 6 12 • seq 6 12 > a.txt • seq 6 12 >> a.txt • less a.txt • less < a.txt • seq 6 12 | less

Slide 22

Slide 22 text

Redirect, Pipe • seq 6 12 • seq 6 12 > a.txt • seq 6 12 >> a.txt • less a.txt • less < a.txt • seq 6 12 | less

Slide 23

Slide 23 text

Redirect, Pipe • seq 6 12 • seq 6 12 > a.txt • seq 6 12 >> a.txt • less a.txt • less < a.txt • seq 6 12 | less

Slide 24

Slide 24 text

Redirect, Pipe • seq 6 12 • seq 6 12 > a.txt • seq 6 12 >> a.txt • less a.txt • less < a.txt • seq 6 12 | less

Slide 25

Slide 25 text

Redirect, Pipe • seq 6 12 • seq 6 12 > a.txt • seq 6 12 >> a.txt • less a.txt • less < a.txt • seq 6 12 | less

Slide 26

Slide 26 text

$ seq 6 12 6 7 8 9 10 11 12

Slide 27

Slide 27 text

$ seq 6 12 > a.a

Slide 28

Slide 28 text

$ seq 6 12 > a.a

Slide 29

Slide 29 text

$ seq 6 12 > a.a $ grep 1 a.a

Slide 30

Slide 30 text

$ seq 6 12 > a.a $ grep 1 a.a

Slide 31

Slide 31 text

$ seq 6 12 > a.a $ grep 1 a.a

Slide 32

Slide 32 text

$ seq 6 12 > a.a $ grep 1 a.a 10 11 12

Slide 33

Slide 33 text

$ seq 6 12 > a.a $ grep 1 a.a 10 11 12 $ grep 1

Slide 34

Slide 34 text

$ seq 6 12 > a.a $ grep 1 a.a 10 11 12 $ grep 1

Slide 35

Slide 35 text

$ seq 6 12 > a.a $ grep 1 a.a 10 11 12 $ grep 1

Slide 36

Slide 36 text

$ seq 6 12 > a.a $ grep 1 a.a 10 11 12 $ grep 1

Slide 37

Slide 37 text

$ seq 6 12 > a.a $ grep 1 a.a 10 11 12 $ grep 1

Slide 38

Slide 38 text

$ seq 1 999 | less

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

$ seq 1 999 | e

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

seq 1 999 | e

Slide 43

Slide 43 text

| e ??? seq 1 999 | e

Slide 44

Slide 44 text

| e ??? Really? seq 1 999 | e

Slide 45

Slide 45 text

| e ??? Really? seq 1 999 | e

Slide 46

Slide 46 text

| e ??? Really! seq 1 999 | e

Slide 47

Slide 47 text

$ seq 6 12 > a.a $ seq 6 12 >> a.a

Slide 48

Slide 48 text

Really!

Slide 49

Slide 49 text

https://csvkit.readthedocs.io/ • csvcut • csvgrep • csvclean • csvjoin • csvsort • csvstack • csvformat • csvjson • csvlook • csvpy • csvsql • csvstat • in2csv • sql2csv

Slide 50

Slide 50 text

https://csvkit.readthedocs.io/ • csvcut • csvgrep • csvclean • csvjoin • csvsort • csvstack • csvformat • csvjson • csvlook • csvpy • csvsql • csvstat • in2csv • sql2csv

Slide 51

Slide 51 text

csvcut -c 5,13 input.csv csvcut -c name,post_code input.csv

Slide 52

Slide 52 text

csvcut -c 5,13 input.csv | md5 csvcut -c name,post_code input.csv | md5

Slide 53

Slide 53 text

my_prog > a.txt (edit)

Slide 54

Slide 54 text

my_prog > a.txt (edit) my_prog > b.txt; diff -u {a,b}.txt | wc

Slide 55

Slide 55 text

my_prog > a.txt (edit) my_prog > b.txt; diff -u {a,b}.txt | wc 0

Slide 56

Slide 56 text

my_prog > a.txt (edit) my_prog > b.txt; diff -u {a,b}.txt | wc 0 On Windows, you may need `a.txt b.txt` instead of `{a,b}.txt`

Slide 57

Slide 57 text

my_prog > a.txt (edit) my_prog > b.txt; diff -u {a,b}.txt | wc my_prog > b.txt; diff -u {a,b}.txt >
 ab.diff; wc ab.diff

Slide 58

Slide 58 text

my_prog > a.txt (edit) my_prog > b.txt; diff -u {a,b}.txt | wc my_prog > b.txt; diff -u {a,b}.txt >
 ab.diff; wc ab.diff e ab.diff

Slide 59

Slide 59 text

my_prog > a.txt (edit) my_prog > b.txt; diff -u {a,b}.txt | wc my_prog > b.txt; diff -u {a,b}.txt >
 ab.diff; wc ab.diff e ab.diff mv b.txt a.txt

Slide 60

Slide 60 text

| sort | uniq -c | sort -nr

Slide 61

Slide 61 text

| sort | uniq -c | sort -nr

Slide 62

Slide 62 text

| sort | uniq -c | sort -nr

Slide 63

Slide 63 text

| sort | uniq -c | sort -nr

Slide 64

Slide 64 text

| sort | uniq -c | sort -nr

Slide 65

Slide 65 text

THAT SENTENCE HAD TOO MANY SYLLABLES!
 
 APOLOGIZE!

Slide 66

Slide 66 text

THAT SENTENCE HAD TOO MANY SYLLABLES!
 
 APOLOGIZE!

Slide 67

Slide 67 text

THAT SENTENCE HAD TOO MANY SYLLABLES!
 
 APOLOGIZE!

Slide 68

Slide 68 text

cd perl-5.36.0

Slide 69

Slide 69 text

grep -Rh '#!' . | sort | uniq -c | sort -nr | head -14

Slide 70

Slide 70 text

grep -Rh '#!' . | sort | uniq -c | sort -nr | head -14

Slide 71

Slide 71 text

grep -Rh '#!' . | sort | uniq -c | sort -nr | head -14

Slide 72

Slide 72 text

grep -Rh '#!' . | sort | uniq -c | sort -nr | head -14

Slide 73

Slide 73 text

grep -Rh '#!' . | sort | uniq -c | sort -nr | head -14

Slide 74

Slide 74 text

grep -Rh '#!' . | sort | uniq -c | sort -nr | head -14

Slide 75

Slide 75 text

grep -Rh '#!' . | sort | uniq -c | sort -nr | head -14 556 #!./perl 322 #!/usr/bin/perl -w 300 #!/usr/bin/perl 196 #!perl 178 #!./perl -w 126 #!perl -w 37 #!/bin/sh 31 #!/usr/local/bin/perl 24 #!/pro/bin/perl 20 #!/usr/bin/perl -Tw 19 #!/usr/bin/perl 15 #! /bin/sh 13 #!/usr/bin/perl -wT 13 #!/usr/bin/env perl

Slide 76

Slide 76 text

grep -Rh '#!' . | sort | uniq -c | sort -nr | head -14 556 #!./perl 322 #!/usr/bin/perl -w 300 #!/usr/bin/perl 196 #!perl 178 #!./perl -w 126 #!perl -w 37 #!/bin/sh 31 #!/usr/local/bin/perl 24 #!/pro/bin/perl 20 #!/usr/bin/perl -Tw 19 #!/usr/bin/perl 15 #! /bin/sh 13 #!/usr/bin/perl -wT 13 #!/usr/bin/env perl

Slide 77

Slide 77 text

csvcut -C 1 v_o.csv | sort | uniq -c | sort -nr | e

Slide 78

Slide 78 text

perl -wpE 'if ($. != 1) {s/^"\d+",// or die}' v_o.csv | sort | uniq -c | sort -nr | e

Slide 79

Slide 79 text

__END__ Adapted from original one-liner: perl -wpE 'if ($. != 1) {s/^"\d+",// or die}' v_o.csv | sort | uniq -c | sort -nr

Slide 80

Slide 80 text

How it works • open my $fh, '<', 'my fi le.txt'; • Magic cookie • • • •

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

How it works • open my $fh, '<', 'my fi le.txt'; • Magic cookie • 0,1,2 • STDIN, STDOUT, STDERR • $*IN, $*OUT, $*ERR • Sane defaults, invention of STDERR

Slide 83

Slide 83 text

Peter Piper Processed a Plethora of
 Programs

Slide 84

Slide 84 text

Peter Piper Processed a Plethora of
 Programs, in Parallel !

Slide 85

Slide 85 text

Peter Piper Processed a Plethora of
 Programs, in Parallel ! seq

Slide 86

Slide 86 text

Peter Piper Processed a Plethora of
 Programs, in Parallel ! seq 1000000000

Slide 87

Slide 87 text

Peter Piper Processed a Plethora of
 Programs, in Parallel ! seq 1000000000 | cut -c 1

Slide 88

Slide 88 text

Peter Piper Processed a Plethora of
 Programs, in Parallel ! seq 1000000000 | cut -c 1 | uniq

Slide 89

Slide 89 text

Peter Piper Processed a Plethora of
 Programs, in Parallel ! seq 1000000000 | cut -c 1 | uniq

Slide 90

Slide 90 text

Peter Piper Processed a Plethora of
 Programs, in Parallel ! seq 1000000000 | cut -c 1 | uniq

Slide 91

Slide 91 text

Now You!

Slide 92

Slide 92 text

use autodie; my $input_path = './name1_to_edit_by_hand.txt'; my $output_path = './name2_to_edit_by_hand.txt'; open my $fh_in, '<', $input_path; open my $fh_out, '>', $output_path; while ( my $line = <$fh_in> ) { chomp $line; my $output = do_something_with($line); say {$out_fh} $output; }

Slide 93

Slide 93 text

use autodie; my $input_path = './name1_to_edit_by_hand.txt'; my $output_path = './name2_to_edit_by_hand.txt'; open my $fh_in, '<', $input_path; open my $fh_out, '>', $output_path; while ( my $line = <$fh_in> ) { chomp $line; my $output = do_something_with($line); say {$out_fh} $output; }

Slide 94

Slide 94 text

use autodie; my $input_path = './name1_to_edit_by_hand.txt'; my $output_path = './name2_to_edit_by_hand.txt'; open my $fh_in, '<', $input_path; open my $fh_out, '>', $output_path; while ( my $line = <$fh_in> ) { chomp $line; my $output = do_something_with($line); say {$out_fh} $output; }

Slide 95

Slide 95 text

while (<>) { chomp; say do_something_with($_); } prog ./name1_to_edit_by_hand.txt >./name2_to_edit_by_hand.txt

Slide 96

Slide 96 text

#!/usr/bin/env python3 import fileinput for line in fileinput.input(): line = line.rstrip('\r\n') print(">>", line, "<<") https://docs.python.org/3/library/fileinput.html #!/usr/bin/env perl use 5.36.0; while (<>) { chomp; say ">> $_ <<"; } https://perldoc.perl.org/perlop#I/O-Operators #!/usr/bin/env raku use v6; for lines() { say ">> $_ <<"; } https://docs.raku.org/type/IO::CatHandle

Slide 97

Slide 97 text

#!/usr/bin/env python3 import fileinput for line in fileinput.input(): line = line.rstrip('\r\n') print(">>", line, "<<") https://docs.python.org/3/library/fileinput.html #!/usr/bin/env perl use 5.36.0; while (<>) { chomp; say ">> $_ <<"; } https://perldoc.perl.org/perlop#I/O-Operators #!/usr/bin/env raku use v6; for lines() { say ">> $_ <<"; } https://docs.raku.org/type/IO::CatHandle

Slide 98

Slide 98 text

#!/usr/bin/env python3 import fileinput for line in fileinput.input(): line = line.rstrip('\r\n') print(">>", line, "<<") https://docs.python.org/3/library/fileinput.html #!/usr/bin/env perl use 5.36.0; while (<>) { chomp; say ">> $_ <<"; } https://perldoc.perl.org/perlop#I/O-Operators #!/usr/bin/env raku use v6; for lines() { say ">> $_ <<"; } https://docs.raku.org/type/IO::CatHandle

Slide 99

Slide 99 text

#!/usr/bin/env python3 import fileinput for line in fileinput.input(): line = line.rstrip('\r\n') print(">>", line, "<<") https://docs.python.org/3/library/ fi leinput.html #!/usr/bin/env perl use 5.36.0; while (<>) { chomp; say ">> $_ <<"; } https://perldoc.perl.org/perlop#I/O-Operators #!/usr/bin/env raku use v6; for lines() { say ">> $_ <<"; } https://docs.raku.org/type/IO::CatHandle

Slide 100

Slide 100 text

# XXX Remove after testing !!! my $default_file = 'C:/path/to/trimmed.dat'; push @ARGV, $default_file if not @ARGV;

Slide 101

Slide 101 text

# XXX Remove after testing !!! my $default_file = 'C:/path/to/trimmed.dat'; push @ARGV, $default_file if not @ARGV; @ARGV ||= $default_file;

Slide 102

Slide 102 text

# XXX Remove after testing !!! my $default_file = 'C:/path/to/trimmed.dat'; push @ARGV, $default_file if not @ARGV; @ARGV ||= $default_file; # Fails sometimes

Slide 103

Slide 103 text

Further Reading • https://utcc.utoronto.ca/~cks/space/blog/python/ ProgramFilterVsWrapper
 Programs as wrappers versus fi lters of other programs • http://catb.org/~esr/writings/taoup/html/ ch01s06.html
 Basics of the Unix Philosophy

Slide 104

Slide 104 text

Q & A

Slide 105

Slide 105 text

Command-line Filters: Time to Shine PLEASE DOWNLOAD THESE SLIDES! 
 http://speakerdeck.com/util
 <<< >>> >>> <<<

Slide 106

Slide 106 text

Much Thanks
 to You All!

Slide 107

Slide 107 text

Copyrights

Slide 108

Slide 108 text

Copyright Information: Images and Video • Camelia • © 2009 by Larry Wall
 http://github.com/perl6/mu/raw/master/misc/camelia.txt • Yellow Laundry Ticket • © (assumed) TC Dry Clean Supply of Houston, TX
 Used without permission, even though we are *in* Houston!
 https://drycleansupply.com/product-category/invoice-tag- thermal-ribbon/laundry-ticket-tag/ •

Slide 109

Slide 109 text

Copyright Information: This Talk This work is licensed under a Creative Commons Attribution 4.0 International License. CC BY https://creativecommons.org/licenses/by/4.0/ (email me for the original Apple Keynote .key fi le)

Slide 110

Slide 110 text

History • v 0.98 2022-06-02
 Presented incomplete version to Atlanta Perlmongers
 • v 1.00 2022-06-24
 Presented fi nal version to The Perl and Raku Conference in Houston, TX, USA
 • v 1.01 2023-07-12
 Reprised at The Perl and Raku Conference in Toronto, Ontario, CA