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
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
<<<
>>>
>>>
<<<
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