perl基本命令罗列

perl基本命令罗列:

 

1.查看perl版本

perl -v

 

This is perl,v5.8.3 built for sun4-solaris-thread-multi
    (with 8 registered patches, see 
perl -V for more detail)

    Copyright 1987-2003, Larry Wall

    Binary build 809 provided by ActiveState Corp.
    http://www.ActiveState.com
    ActiveState is a division of Sophos.
    Built Feb  3 2004 00:32:12

    
Perl maybe copied only under the terms of either the Artistic
    License or the GNU General Public License, which may befound in
    the 
Perl 5source kit.

    Complete documentation for 
Perl, including FAQ lists, should be
    found on this system using `man 
perl' or `perldoc perl'.  If you
    have access to the Internet, point your browser at
    http://www.
perl.com/,the Perl Home Page.

 

2.查看某个模块

格式:perl modleName

eg. perl CGI

 

3.查看某个sub函数

格式:perl -f subname

eg. perldoc -f localtime
localtime EXPR
localtime
        Converts a time as returned by thetime function to a 9-element
        list with the time analyzed for thelocal time zone. Typically
        used as follows:
           #  0    1    2    3     4    5    6     7     8
           ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
<continues>

 

 

 

 

本文和大家重点讨论一下Perl命令行常见用法及技巧,主要包括替换,搜索等内容,希望通过本文的学习你对Perl命令行的用法有明确的认识。

Perl命令行常见用法及技巧

Perl命令行之替换

◆将所有C程序中的foo替换成bar,旧文件备份成.bak

perl -p -i.bak -e 's/\bfoo\b/bar/g' *.c

很强大的功能,特别是在大程序中做重构。记得只有在UltraEdit用过。如果你不想备份,就直接写成perl-p-i-e或者更简单perl-pie,恩,pie这个单词不错

◆将每个文件中出现的数值都加一

perl -i.bak –pe 's/(\d+)/1+$1/ge'file1file2….

◆将换行符\r\n替换成\n

perl-pie's/\r\n/\n/g'file

同dos2unix命令。

◆将换行符\n替换成\r\n

perl-pie's/\n/\r\n/g'file

同unix2dos命令。


取出文件的一部分

显示字段0-4和字段6,字段的分隔符是空格

perl-lane'print"@F[0..4]$F[6]"'file

很好很强大,同awk'print$1,$2,$3,$4,$5,$7'。参数名称lane也很好记。

◆如果字段分隔符不是空格而是冒号,则用perl-F:-lane'print"@F[0..4]\n"'/etc/passwd

显示START和END之间的部分

perl-ne'printif/^START$/../^END$/'file

恐怕这个操作只有sed才做得到了吧……

相反,不显示START和END之间的部分

perl-ne'printunless/^START$/../^END$/'file

显示开头50行:

perl-pe'exitif$.>50'file

同命令head-n50

不显示开头10行:perl-ne'printunless1..10'file

显示15行到17行:perl-ne'printif15..17'file

每行取前80个字符:perl-lne'printsubstr($_,0,80)=""'file

每行丢弃前10个字符:perl-lne'printsubstr($_,10)=""'file


Perl命令行之搜索

◆查找comment字符串:perl-ne'printif/comment/'duptext

这个就是普通的grep命令了。

◆查找不含comment字符串的行:perl-ne'printunless/comment/'duptext

反向的grep,即grep-v。

◆查找包含comment或apple的行:

perl-ne'printif/comment/||/apple/'duptext,相同的功能就要用到egrep了。


Perl命令行之计算

计算字段4和倒数第二字段之和:

perl-lane'print$F[4]+$F[-2]'

要是用awk,就得写成awk'{i=NF-1;print$5+$i}'


Perl命令行之排序和反转

文件按行排序:perl-e'printsort<>'file

相当于简单的sort命令。

◆文件按段落排序:Perl-00-e'printsort<>'file

多个文件按文件内容排序,并返回合并后的文件:

perl-0777-e'printsort<>'file1file2

◆文件按行反转:Perl-e'printreverse<>'file1

相应的命令有吗?有……不过挺偏,tac(cat的反转)


Perl命令行之数值计算

10进制转16进制:

perl-ne'printf"x\n",$_'

10进制转8进制:perl-ne‘printf“%o\n”,$_’

16进制转10进制:

perl-ne'printhex($_)."\n"'

8进制转10进制:

perl-ne'printoct($_)."\n"'

简易计算器。

perl-ne'printeval($_)."\n"'


其他

启动交互式perl

perl-de1

查看包含路径的内容:

perl-le'printfor@INC'


备注

与One-Liner相关的Perl命令行参数:

-0<数字>

(用8进制表示)指定记录分隔符($/变量),默认为换行

-00,段落模式,即以连续换行为分隔符

-0777,禁用分隔符,即将整个文件作为一个记录

-a,自动分隔模式,用空格分隔$_并保存到@F中。相当于@F=split。分隔符可以使用-F参数指定

-F,指定-a的分隔符,可以使用正则表达式

-e,执行指定的脚本。

-i<扩展名>原地替换文件,并将旧文件用指定的扩展名备份。不指定扩展名则不备份。

-l,对输入内容自动chomp,对输出内容自动添加换行

-n,自动循环,相当于while(<>){脚本;}

-p,自动循环+自动输出,相当于while(<>){脚本;print;}

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值