perl连接字符串_学习一点perl单行命令知识

76c62657de66ac9b6734ad85bed969a1.png81d8dfd21f9c90684ab584a59a0c276d.png 今天是生信星球陪你的第566天76c62657de66ac9b6734ad85bed969a1.png


   大神一句话,菜鸟跑半年。我不是大神,但我可以缩短你走弯路的半年~

   就像歌儿唱的那样,如果你不知道该往哪儿走,就留在这学点生信好不好~

   这里有豆豆和花花的学习历程,从新手到进阶,生信路上有你有我!

豆豆写于2020.3.13啊,这两天收拾新家,真是累并快乐着!每天都要拿几十个快递~_~
从地基到封顶,从精装到收楼,每次都在期盼。不过这一天终于来了
住进来顿时踏实了,也准备迎接第一批客人啦~~
安心工作,舒心生活

推荐一本书:《Perl One-liners:130 Programs That Get Things Done》Perl one-liners will make you a shell warrior生信~一天认识Perl,你相信吗?
迟到的邂逅--perl

必须了解的参数

  • -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'

?

初学生信,很荣幸带你迈出第一步

?生信星球 ?一个不拽术语、通俗易懂的生信知识平台

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值