• PERL命令行


    PERL命令行

    perl命令行操作需要加上-e否则会寻找perl脚本文件,命令用单引号包围,双引号会被OS认为是变量

    PERL命令行参数

    1. -n参数

      -n参数相当于创建while循环

      while(<>) {
      	commands;
      }
      
    2. -p参数

      -p参数创建while循环,下一步打印

      while(<>) {
      	command;
      } continue {
      	print or die
      }
      
    3. -i参数

      直接对操作对象进行修改

    perl特殊变量

    Special variable Meaning
    $_ 默认的输入和模式搜索空间
    $. 读取的最后一个文件柄的当前输入行号
    $/ 输入记录的分隔符,默认为换行。
    $ 输出记录的分隔符,默认为换行。
    $, 打印输出字段分隔符
    $‘’ 连接字符串中插值的数组元素的分隔符
    $0 正在执行的Perl脚本的文件名称

    换行

    -n和-p选项虽然仅有很小的差别,但是产生的结果会有很大的不同

    I Do Not Love You Except Because I Love You :
    I do not love you except because I love you;
    I go from loving to not loving you,
    From waiting to not waiting for you
    My heart moves from cold to fire.
    I love you only because it's you the one I love;
    I hate you deeply, and hating you
    Bend to you, and the measure of my changing love for you
    Is that I do not see you but love you blindly.
    Maybe January light will consume
    My heart with its cruel
    Ray, stealing my key to true calm.
    In this part of the story I am the one who
    Dies, the only one, and I will die of love because I love you,
    Because I love you, Love, in fire and blood.
    

    image.png

    双倍行距

    设定输出分隔符

    perl -pe '$/ = "
    "' poem
    

    image.png

    当前读入行追加换行

    perl -pe '$_ .= "
    "' poem
    
    or 
    
    perl -ne 'print $_ .= "
    "' poem
    

    空行外两倍行距

    匹配文本开头存在的行

    perl -pe '$_ .= "
    " if /./'
    

    排除空行

    perl -pe '$_ .= "
    " unless /^$/' poem
    

    多倍行距

    perl -pe '$_ .= "
    "x4 if /./' poem
    

    移除空行

    perl -ne 'print  if /./' poem
    or
    perl -ne 'print  unless /^$/' poem
    

    文本单词间两倍间距

    perl -pe 's/ /  /g' poem
    

    移除单词间的空格

    perl -pe 's/ +//g' poem
    

    编号

    所有行进行编号

    perl -pe '$_ = "$. $_"' poem
    or
    perl -ne 'print $. $_' poem
    

    非空行编号空行不保留

    perl -ne 'print "$. $_" if /./' poem
    or
    perl -ne 'print "$. $_" if /S/' poem
    or
    perl -ne 'print "$. $_" unless /^$/' poem
    

    上述命令执行的结果是文本原始的行数编号,所以不连续

    perl -ne 'print ++$x." $_" if /./' poem
    or
    perl -ne 'print ++$x." $_" if /S/' poem
    or
    perl -ne 'print ++$x." $_" unless /^$/' poem
    

    非空行编号空行保留

    perl -pe '$_ = ++$x." $_" if /./'
    or
    perl -pe '$_ = ++$x." $_" if /S/'
    or
    perl -pe '$_ = ++$x." $_" unless /^$/'
    

  • 相关阅读:
    rtmp 之 amf
    Codeforces Round #601 (Div. 1)
    Codeforces Round #618 (Div. 1)
    Codeforces Round #694 (Div. 1) BCDE
    AtCoder Regular Contest 106 DEF
    AtCoder Grand Contest 006 BCDEFF
    JavaScript中深拷贝的实现方法
    suiidfadf
    macOs打开时提示:xxx.app已损坏修复教程
    vue通过事件对象获取标签上的属性值
  • 原文地址:https://www.cnblogs.com/movit/p/14906899.html
Copyright © 2020-2023  润新知