Chapter 6: Perl One-Liners:Text Conversion and Substitution

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]}"'
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,434评论 0 10
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc阅读 2,924评论 0 0
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,504评论 0 13
  • 【香知蜜读885】 2018/05/18 星期五 荐书蜜友:童言·果语 文:李爱玲 01 这两天,一则新闻上了热...
    自律时光阅读 1,940评论 0 0
  • 不要去听别人的忽悠,你人生的每一步都必须靠自己的能力完成。自己肚子里没有料,手上没本事,认识 再多人也没用。人脉只...
    疏碧湖的砍柴人阅读 89评论 0 1