美文网首页
Chapter 6: Perl One-Liners:Text

Chapter 6: Perl One-Liners:Text

作者: 周运来就是我 | 来源:发表于2018-09-12 10:44 被阅读22次

    In this chapter, we’ll look at various one-liners that

    • change, convert, and substitute text,
    • including base64 encoding and decoding,
    • URL escaping andunescaping,
    • HTML escaping and unescaping,
    • converting text case,
    • reversing lines.

    You’ll also get to know the y, tr, uc, lc, and reverse operators and string-escape sequences.

    6.1 ROT13 a string
          perl -le '$string = "bananas"; $string =~ y/A-Za-z/N-ZA-Mn-za-m/; print $string'
    

    To ROT13 the whole file bananas.txt and print it to the screen, just do this:

      perl -lpe 'y/A-Za-z/N-ZA-Mn-za-m/' bananas.txt
    

    You can also use Perl’s -i argument to do in-place replacement of the file. For example, to ROT13 oranges.txt in-place, write this:

        perl -pi.bak -e 'y/A-Za-z/N-ZA-Mn-za-m/' oranges.txt
    
    6.2 Base64-encode a string
        perl -MMIME::Base64 -e 'print encode_base64("string")'
    

    To base64-encode the whole file, use this:

      perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)' file
    
    6.3 Base64-decode a string
        perl -MMIME::Base64 -le 'print decode_base64("base64string")'
    
    6.4 URL -escape a string
        perl -MURI::Escape -le 'print uri_escape("http://example.com")'
    
    6.5 URL -unescape a string
            perl -MURI::Escape -le 'print uri_unescape("http%3A%2F%2Fexample.com")'
    
    6.6 HTML -encode a string
        perl -MHTML::Entities -le 'print encode_entities("<html>")'
    
    6.7 HTML -decode a string
          perl -MHTML::Entities -le 'print decode_entities("&lt;html&gt;")'
    
    6.8 Convert all text to uppercase
      perl -nle 'print uc'
    

    Or you can apply the \U escape sequence to string interpolation:

      perl -nle 'print "\U$_"'
    
    6.9 Convert all text to lowercase
        perl -nle 'print lc'
    
    6.10 Uppercase only the first letter of each line
        perl -nle 'print ucfirst lc'
    

    You can do the same thing using escape codes and string interpolation:

        perl -nle 'print "\u\L$_"'
    
    6.11 Invert the letter case
          perl -ple 'y/A-Za-z/a-zA-Z/'
    
    6.12 Title-case each line
        perl -ple 's/(\w+)/\u$1/g'
    
    6.13 Strip leading whitespace (spaces, tabs) from the beginning of each line
        perl -ple 's/^[ \t]+//'
    
    6.14 Strip trailing whitespace (spaces, tabs) from the end of each line
          perl -ple 's/[ \t]+$//'
    
    6.15 Strip whitespace (spaces, tabs) from the beginning and end of each line
            perl -ple 's/^[ \t]+|[ \t]+$//g'
    
    6.16 Convert UNIX newlines to DOS/Windows newlines
          perl -pe 's|\012|\015\012|'
    
    6.17 Convert DOS/Windows newlines to UNIX newlines
        perl -pe 's|\015\012|\012|'
    
    6.18 Convert UNIX newlines to Mac newlines
            perl -pe 's|\012|\015|'
    
    6.19 Substitute (find and replace) “foo” with “bar” on each line
        perl -pe 's/foo/bar/'
    
    6.20 Substitute (find and replace) “foo” with “bar” on lines that match “baz”
          perl -pe '/baz/ && s/foo/bar/'
    
    6.21 Print paragraphs in reverse order
            perl -00 -e 'print reverse <>' file
    
    6.22 Print all lines in reverse order
        perl -lne 'print scalar reverse $_'
    
    6.23 Print columns in reverse order
        perl -alne 'print "@{[reverse @F]}"'
    

    Notice, however, that the : characters are missing in this output. To get them back, you need to modify the one-liner a bit and set the $" variable to ":", as shown here:

          perl -F: -alne '$" = ":"; print "@{[reverse @F]}"'
    

    相关文章

      网友评论

          本文标题:Chapter 6: Perl One-Liners:Text

          本文链接:https://www.haomeiwen.com/subject/oasftftx.html