美文网首页生物信息学
学习一点perl单行命令知识

学习一点perl单行命令知识

作者: 刘小泽 | 来源:发表于2020-03-13 23:30 被阅读0次

刘小泽写于2020.3.13
推荐一本书:《Perl One-liners:130 Programs That Get Things Done》
Perl one-liners will make you a shell warrior

必须了解的参数

  • -e:表示运行(execute)

  • -p:运行后打印输出

  • -i:直接将修改内容替换原文(很像sed的-i参数)
    如果感觉不保险,可以使用-i.bak来做个备份,这样perl先会创建一个bak备份文件,然后再修改源文件:perl -pi.bak -e 's/you/me/g' file1 file2 file3

  • -n:表示循环运行每一行,但和-p不同,它不自动输出(要输出需要和print连用)

  • -a:自动将一行的内容按空格分隔(默认是空格)

  • -F:修改分隔符

  • -l:加换行符 (remove the newline at the end)

  • $_:表示当前行

  • $.:当前行号

  • $\:输出分隔符(就像AWK的OFS)
    prints the contents of $_ and appends $\ at the end of the output

  • .:连接两个字符串

  • unless:if not

  • -s : 任意空白字符

  • -S: 非空白字符

perl的一个强大之处在于它的模块库:CPAN,包括超过100,000的模块,其中一个就是List::Util,用来调用其他的实用功能(例如sum)。不过这个不需要安装,它是perl自带的

基础的语句

只有匹配到带有特定字符的行,才替换

perl -pi -e 's/you/me/g if /we/' file

只有包含数字的行,才替换

perl -pi -e 's/you/me/g if/\d/' file

统计出现重复的行

perl -ne 'print if $a{$_}++' file

得到每行的行号

默认使用空格,如果把$.$_放一起,就是空格分隔,左边是行号,右边是每行信息;当然可以用其他分隔符,例如:$.:$_

perl -ne 'print "$. $_"' fle

它等同于:perl -pe '$_ = "$. $_"' file ,就是把$. $_的内容赋值给了原来的$_,然后再输出

得到重复的行和行号 =》 组合上面两个单行命令

perl -ne 'print "$. $_" if $a{$_}++'

加和
perl -MList::Util=sum -alne 'print sum @F'
12 13 # 这里输入
25 #这里输出

其中-MList::Util 表示导入模块;-a参数把输入的内容按空格分隔后,存到了@F中,然后求和

找某天的日期
# 100天前
perl -MPOSIX -le '@t = localtime; $t[3] -= 100; print scalar localtime mktime @t'
# 100天后
perl -MPOSIX -le '@t = localtime; $t[3] += 100; print scalar localtime mktime @t'
获得8位字母的密码
perl -le 'print map { ("a".."z")[rand 26] } 1..8'

"a".."z"获得a-z字母;随机挑一个;挑了8次

数学计算
perl -lane '$sum += $F[0]; END { print $sum }'

-a参数将行拆分成许多fields,存到@F中;然后将第一列$F[0] 加和:$sum+=$F[0] ;运行结束后,输出最后结果

以冒号为分隔符,获取第一列
perl -F: -alne 'print $F[0]' file

加个换行符(多空出来一行)

# 方法一:$\ = OFS in awk (output filed seperator)
perl -pe '$\ = "\n"' file
# 方法二:
perl -pe 'BEGIN { $\ = "\n" }' file
# 方法三:
perl -pe '$_ .= "\n"' file
# 方法四(最清爽的方法):
perl -pe 's/$/\n/' file

加两个换行符是这样

# 方法1. set output seperator 
perl -pe '$\ = "\n\n"'
# 方法2. connect at last
perl -pe '$_ .= "\n\n"'
# 方法3. replace
perl -pe 's/$/\n\n/'

一般单行命令是通过管道接受前面的结果,然后进行处理

打印同一字符串多次

# 1. inserts seven newlines after each line
perl -pe '$_ .= "\n"x7'
# 2. print several times
perl -e 'print "foo"x5'
# or
perl -e 'print "a"x1024'

行前加换行符

# s/regex/replace/
perl -pe 's/^/\n/'

移除空行

Note:Windows uses two characters for the newline.

# -n just execute not print(-p)
perl -ne 'print unless /^$/'

or

# use -l (chomp automatically, to remove the newline at the end)
perl -lne 'print if length'
# means: print the line if it has some length

or remove all spaces/tab (if contains any white character)

perl -ne 'print if /\S/'

Perl’s motto is There’s More Than One Way To Do It 【TIMTOWTDI and pronounced “Tim Toady“】

去掉空格

perl -pe 's/ +//g'

or remove all spaces, tabs, newlines...

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

or change multi spaces into one

perl -pe 's/ +/ /g'

欢迎关注我们的公众号~_~  
我们是两个农转生信的小硕,打造生信星球,想让它成为一个不拽术语、通俗易懂的生信知识平台。需要帮助或提出意见请后台留言或发送邮件到jieandze1314@gmail.com

Welcome to our bioinfoplanet!

相关文章

网友评论

    本文标题:学习一点perl单行命令知识

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