Perl Wiki
Ahy1 (talk | contribs)
(→‎sed: + example)
Ahy1 (talk | contribs)
m (formatting)
Line 5: Line 5:
 
{| width="100%" style="border:1px solid black; margin-left: .5em;"
 
{| width="100%" style="border:1px solid black; margin-left: .5em;"
 
|- style="background-color:#C8FCFF;"
 
|- style="background-color:#C8FCFF;"
| width="20%" | task || width="50%" | sed || perl
+
| width="25%" | task || width="40%" | sed || perl
 
|-
 
|-
 
| Replace ''12'' with ''Twelve'' || <code>sed 's/12/twelve/g'</code> || <code>perl -pe 's/12/twelve/g'</code>
 
| Replace ''12'' with ''Twelve'' || <code>sed 's/12/twelve/g'</code> || <code>perl -pe 's/12/twelve/g'</code>
Line 19: Line 19:
 
{| width="100%" style="border:1px solid black; margin-left: .5em;"
 
{| width="100%" style="border:1px solid black; margin-left: .5em;"
 
|- style="background-color:#C8FCFF;"
 
|- style="background-color:#C8FCFF;"
| width="30%" | task || width="30%" | awk || perl
+
| width="25%" | task || width="40%" | awk || perl
 
|-
 
|-
 
| Print second field (whitespace-separated) || <code>awk '{print $2}'</code> || <code>perl -lane 'print $F[1]'</code>
 
| Print second field (whitespace-separated) || <code>awk '{print $2}'</code> || <code>perl -lane 'print $F[1]'</code>
Line 29: Line 29:
 
{| width="100%" style="border:1px solid black; margin-left: .5em;"
 
{| width="100%" style="border:1px solid black; margin-left: .5em;"
 
|- style="background-color:#C8FCFF;"
 
|- style="background-color:#C8FCFF;"
| width="30%" | task || width="30%" | tr || perl
+
| width="25%" | task || width="40%" | tr || perl
 
|-
 
|-
 
| ROT13 || <code>tr 'A-Za-z' 'N-ZA-Mn-za-m'</code> || <code>perl -pe 'y/A-Za-z/N-ZA-Mn-za-m/'</code>
 
| ROT13 || <code>tr 'A-Za-z' 'N-ZA-Mn-za-m'</code> || <code>perl -pe 'y/A-Za-z/N-ZA-Mn-za-m/'</code>
Line 40: Line 40:
 
{| width="100%" style="border:1px solid black; margin-left: .5em;"
 
{| width="100%" style="border:1px solid black; margin-left: .5em;"
 
|- style="background-color:#C8FCFF;"
 
|- style="background-color:#C8FCFF;"
| width="30%" | task || width="30%" | grep || perl
+
| width="25%" | task || width="40%" | grep || perl
 
|-
 
|-
 
| Print only lines containing ''12'' || <code>grep '12'</code> || <code>perl -nle 'print if /12/'</code>
 
| Print only lines containing ''12'' || <code>grep '12'</code> || <code>perl -nle 'print if /12/'</code>

Revision as of 22:58, 19 July 2006

This article compares how simple tasks are done using UNIX tools and Perl one-liners.

sed

task sed perl
Replace 12 with Twelve sed 's/12/twelve/g' perl -pe 's/12/twelve/g'
Replace the word sh with Bourne Shell sed -e 's/ sh / Bourne Shell /g' (Won't match words at start/end of line) perl -pe 's/\bsh\b/Bourne Shell/g'
Remove lines 2 to 4 from stream sed '2,4d' perl -nle 'print if $.<2 || $.>4'

awk

task awk perl
Print second field (whitespace-separated) awk '{print $2}' perl -lane 'print $F[1]'

tr

task tr perl
ROT13 tr 'A-Za-z' 'N-ZA-Mn-za-m' perl -pe 'y/A-Za-z/N-ZA-Mn-za-m/'
Remove carriage return from DOS files tr -d '\r' perl -pe 's/\r//g'

grep

task grep perl
Print only lines containing 12 grep '12' perl -nle 'print if /12/'
Print only lines not containing 12 grep -v '12' perl -nle 'print if !/12/'