Perl Wiki
Ahy1 (talk | contribs)
m (→‎awk: <code> tags)
No edit summary
 
(3 intermediate revisions by 3 users not shown)
Line 3: Line 3:
 
== sed ==
 
== sed ==
   
{| 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="25%" | task || width="40%" | 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''
| Replace the word ''sh'' with ''Bourne Shell'' || <code>sed -e 's/ sh / Bourne Shell /g'</code> <ref>Won't match words at start/end of line</ref>|| <code>perl -pe 's/\bsh\b/Bourne Shell/g'</code>
 
  +
|<code>sed 's/12/twelve/g'</code>
  +
|<code>perl -pe 's/12/twelve/g'</code>
 
|-
 
|-
  +
|Replace the word ''sh'' with ''Bourne Shell''
| Remove lines 2 to 4 from stream || <code>sed '2,4d'</code> || <code><nowiki>perl -nle 'print if $.<2 || $.>4'</nowiki></code>
 
 
|<code>sed -e 's/ sh / Bourne Shell /g'</code><ref>Won't match words at start/end of line</ref>
  +
|<code>perl -pe 's/\bsh\b/Bourne Shell/g'</code><ref>Will match any perl ''word-boundary'' which consists of A-Za-z_ followed by a non A-Za-z_</ref>
 
|-
 
|-
  +
|Remove lines 2 to 4 from stream
  +
|<code>sed '2,4d'</code>
 
|<code><nowiki>perl -nle 'print if $.<2 || $.>4'</nowiki></code>
 
|}
 
|}
   
Line 45: Line 52:
 
| width="25%" | task || width="40%" | 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>[http://programmingbulls.com/perl-grep grep ]'12'</code> || <code>perl -nle 'print if /12/'</code>
 
|-
 
|-
 
| Print only lines not containing ''12'' || <code>grep -v '12'</code> || <code>perl -nle 'print if !/12/'</code>
 
| Print only lines not containing ''12'' || <code>grep -v '12'</code> || <code>perl -nle 'print if !/12/'</code>
|-
 
 
|}
 
|}
   
Line 57: Line 63:
 
| width="25%" | task || width="40%" | nl || perl
 
| width="25%" | task || width="40%" | nl || perl
 
|-
 
|-
| Insert line numbers (lined up)|| nl -ba || perl -nle 'printf "%6s %s\n", $., $_'
+
| Insert line numbers (lined up)|| <code>nl -ba</code> || <code>perl -nle 'printf "%6s %s\n", $., $_'</code>
 
|}
 
|}
   
Line 63: Line 69:
   
 
<references/>
 
<references/>
 
 
 
 
[[Category:Intro]]
 
[[Category:Intro]]

Latest revision as of 12:05, 10 August 2011

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'[1] perl -pe 's/\bsh\b/Bourne Shell/g'[2]
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]'
Count lines starting with X awk '/^X/ {++x} END {print x}' perl -nle '++$x if /^X/; print $x if eof'
Add numbers in second column and print sum awk '{sum+=$2} END {print sum}' perl -lane '$sum+=$F[1]; print $sum if eof'

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 [3] tr -d '\r' perl -pe 'tr/\r//d'

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

nl[]

task nl perl
Insert line numbers (lined up) nl -ba perl -nle 'printf "%6s %s\n", $., $_'

Footnotes[]

  1. Won't match words at start/end of line
  2. Will match any perl word-boundary which consists of A-Za-z_ followed by a non A-Za-z_
  3. This method will remove all carriage return characters, not only those at end of line