设为首页 收藏本站
查看: 996|回复: 0

[经验分享] Perl One-liners学习笔记1

[复制链接]

尚未签到

发表于 2015-12-27 14:06:04 | 显示全部楼层 |阅读模式
http://www.catonmat.net/blog/perl-one-liners-explained-part-one/
File Spacing
1. Double space a file.
perl -pe '$\="\n"'
This one-liner double spaces a file. There are three things to explain in this one-liner. The "-p" and "-e" command line options, and the "$\" variable.
First let's start with the "-e" option. The "-e" option can be used to enter a Perl program directly in the command line. Typically you don't want to create source files for every small program. By using "-e" you can handily specify the program to execute on the command line.
Next the "-p" switch. Specifying "-p" to a Perl program causes it to assume the following loop around your program:
while (<>) {
# your program goes here
} continue {
print or die "-p failed: $!\n";
}
This construct loops over all the input, executes your code and prints the value of "$_". This way you can effectively modify all or some lines of input. The "$_" variable can be explained as an anonymous variable that gets filled with the good stuff.
The "$\" variable is similar to ORS in Awk. It gets appended after every "print" operation. Without any arguments "print" prints the contents of "$_" (the good stuff).
In this one-liner the code specified by "-e" is '$\="\n"', thus the whole program looks like this:
while (<>) {
$\ = "\n";
} continue {
print or die "-p failed: $!\n";
}
What happens is that after reading each line, "$_" gets filled with it (including the existing line's newline), the "$\" gets set to a newline itself and "print" is called. As I already mentioned, without any arguments "print" prints the contents of "$_" and "$\" gets appended at the end. The result is that each line gets printed unmodified and it's followed by the "$\" which has been set to newline. The input has been double-spaced.
There is actually no need to set "$\" to newline on each line. It was just the shortest possible one-liner that double-spaced the file. Here are several others that do the same:
perl -pe 'BEGIN { $\="\n" }'
This one sets the "$\" to newline just once before Perl does anything (BEGIN block gets executed before everything else).
perl -pe '$_ .= "\n"'
This one-liner is equivalent to:
while (<>) {
$_ = $_ . "\n"
} continue {
print or die "-p failed: $!\n";
}
It appends another new-line at the end of each line, then prints it out.
The cleanest and coolest way to do it is probably use the substitution "s///" operator:
perl -pe 's/$/\n/'
It replaces the regular expression "$" that matches at the end of line with a newline, effectively adding a newline at the end.
2. Double space a file, except the blank lines.
perl -pe '$_ .= "\n" unless /^$/'
This one-liner double spaces all lines that are not completely empty. It does it by appending a newline character at the end of each line that is not blank. The "unless" means "if not". And "unless /^$/" means "if not 'beginning is end of line'". The condition "beginning is end of line" is true only for lines that contain the newline character.
Here is how it looks when expanded:
while (<>) {
if ($_ !~ /^$/) {
$_ .= "\n"
}
} continue {
print or die "-p failed: $!\n";
}
A better test that takes spaces and tabs on the line into account is this one:
perl -pe '$_ .= "\n" if /\S/'
Here the line is matched against "\S". "\S" is a regular expression that is the inverse of "\s". The inverse of "\s" is any non-whitespace character. The result is that every line that has at least one non-whitespace character (tab, vertical tab, space, etc) gets double spaced.
3. Triple space a file.
perl -pe '$\="\n\n"'
Or
perl -pe '$_.="\n\n"'
They are the same as one-liner #1, except that two new-lines get appended after each line.
4. N-space a file.
perl -pe '$_.="\n"x7'
This one-liner uses inserts 7 new lines after each line. Notice how I used '"\n" x 7' to repeat the newline char 7 times. The "x" operator repeats the thing on the left N times.
For example:
perl -e 'print "foo"x5'
would print "foofoofoofoofoo".
5. Add a blank line before every line.
perl -pe 's//\n/'
This one-liner uses the "s/pattern/replacement/" operator. It substitutes the first pattern (regular expression) in the "$_" variable with the replacement. In this one-liner the pattern is empty, meaning it matches any position between chars (and in this case it's the position before first char) and replaces it with "\n". The effect is that a newline char gets inserted before the line.
6. Remove all blank lines.
perl -ne 'print unless /^$/'
This one-liner uses "-n" flag. The "-n" flag causes Perl to assume to following loop around your program:
LINE:
while (<>) {
# your program goes here
}
What happens here is that each line gets read by the diamond "<>" operator and is placed in the dolar underscore special variable "$_". At this moment you are free to do with it whatever you want. You specify that in your main program text.
In this one-liner the main program is "print unless /^$/", it gets inserted in the while loop above and the whole Perl program becomes:
LINE:
while (<>) {
print unless /^$/
}
Unraveling it further:
LINE:
while (<>) {
print $_ unless $_ =~ /^$/
}
This one-liner prints all non-blank lines.
A few other ways to do the same:
perl -lne 'print if length'
This one uses the "-l" command line argument. The "-l" automatically chomps the input line (basically gets rid of newline at the end). Next the line is tested for its length. If there are any chars left, it evals to true and the line gets printed.
perl -ne 'print if /\S/'
This one-liner behaves differently than the "print unless /^$/" one-liner. The "print unless /^$/" one-liner prints lines with spaces and tabs, this one doesn't.
7. Remove all consecutive blank lines, leaving just one.
perl -00 -pe ''
Ok, this is really tricky, ain't it? First of all it does not have any code, the -e is empty. Next it has a silly -00 command line option. This command line option turns paragraph slurp mode on. A paragraph is text between two newlines. All the other newlines get ignored. The paragraph gets put in "$_" and the "-p" option prints it out.
Later I found a shorter version of this one-liner:
perl -00pe0
8. Compress/expand all blank lines into N consecutive ones.
perl -00 -pe '$_.="\n"x4'
This one-liner combines the previous one and one-liner #4. It slurps lines paragraph wise, then appends (N-1) new-line. This one-liner expands (or compresses) all new-lines to 5 ("\n" x 4 prints four, and there was one at the end of paragraph itself, so 5).


  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-156996-1-1.html 上篇帖子: perl实现HTML2TXT中文转换 下篇帖子: 查看安装的perl模块
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表