perl基本命令罗列

perl基本命令罗列,第1张

概述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 b

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>

@H_419_186@ 

@H_419_186@ 

@H_419_186@ 

@H_419_186@ 

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

Perl命令@H_419_186@行常见用法及技巧

Perl命令@H_419_186@行之替换

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

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

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

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

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

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

@H_419_186@perl-pIE's/\r\n/\n/g'file

@H_419_186@同dos2unix命令。

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

@H_419_186@perl-pIE's/\n/\r\n/g'file

@H_419_186@同unix2dos命令。

@H_419_186@
@H_419_186@取出文件的一部分

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

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

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

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

@H_419_186@显示START和END之间的部分

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

@H_419_186@恐怕这个 *** 作只有sed才做得到了吧……

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

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

@H_419_186@显示开头50行:

@H_419_186@perl-pe'exitif$.>50'file

@H_419_186@同命令head-n50

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

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

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

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

@H_419_186@
Perl命令@H_419_186@行之搜索

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

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

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

@H_419_186@反向的grep,即grep-v。

@H_419_186@◆查找包含comment或apple的行:

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

@H_419_186@
Perl命令@H_419_186@行之计算

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

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

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

@H_419_186@
Perl命令@H_419_186@行之排序和反转

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

@H_419_186@相当于简单的sort命令。

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

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

@H_419_186@perl-0777-e'printsort<>'file1file2

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

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

@H_419_186@
Perl命令@H_419_186@行之数值计算

@H_419_186@10@H_419_186@进制转16进制:

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

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

@H_419_186@16@H_419_186@进制转10进制:

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

@H_419_186@8@H_419_186@进制转10进制:

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

@H_419_186@简易计算器。

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

@H_419_186@
@H_419_186@其他

@H_419_186@启动交互式perl:

@H_419_186@perl-de1

@H_419_186@查看包含路径的内容:

@H_419_186@perl-le'printfor@INC'

@H_419_186@
@H_419_186@备注

@H_419_186@与One-liner相关的Perl命令@H_419_186@行参数:

@H_419_186@-0<@H_419_186@数字>

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

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

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

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

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

@H_419_186@-e@H_419_186@,执行指定的脚本。

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

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

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

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

总结

以上是内存溢出为你收集整理的perl基本命令罗列全部内容,希望文章能够帮你解决perl基本命令罗列所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1277681.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存