我们在看电子书时,有时遇到源代码,想直接复制过来就可以运行,但一般书中为了便于讲解,会在源代码文件中每行都加上行符,如果代码很长时,自己复制过来再在文件中手功一行一行地把前导行号删除是很麻烦的.
这里提供一个用Perl写的脚本,可以自动把前导行号去掉.
使用方法: ./delnum.pl sourceFile NewFile
其中sourceFile是有行号的源代码文件,NewFile是去掉行号的新文件.
限制:需要把源代码文件放在当前目录,默认的新文件也是在当前目录创建的,另外请确认新文件名不要与已有的文件名重名,否则会覆盖你的文件.
#!/usr/bin/perl # delete the line number mark of lines in a code scource file if($#ARGV != 1){ print "ERROR: Bad arguments ! \n"; print "USAGE: delnum.pl sourceFileName NewFileName\n"; exit 1; } $oldfile = $ARGV[0]; open OLDFILE , "<./$oldfile" or die "Cannot open file $oldfile $!"; @lines=(<OLDFILE>); $newfile = $ARGV[1]; open NEWFILE , ">./$newfile" or die "Cannot create file $newfile $!"; foreach(@lines){ $_=~s/^\d+\s?//;#正则表达式,把行首的数字替换为空字符,即去掉 print NEWFILE "$_"; } close OLDFILE ; close NEWFILE ;
相关资源:Java 面经手册·小傅哥(公众号:bugstack虫洞栈).pdf