open my $input, '<', shift or die $!;
while (<$input>) {
...
# 2 楼答案
在perl中:
use strict;
use warnings;
my $line = 'what to be searched';
open my $fh, '<', '/path/to/the/file' or die "unable to open file: $!";
while(<$fh>) {
chomp;
if ($_ eq $line) {
print "found $line at line $.\n";
last;
}
}
use strict;
use warnings;
open INFILE, "<infile" or die;
open OUTFILE, ">outfile" or die;
while(<INFILE>) {
$_=~s/source-regex/replace-with/g;
print OUTFILE;
}
# 1 楼答案
作为一行:
作为脚本:
用法:
perl script.pl input.txt > output.txt
有很多方法可以优化这一点,但利用您提供的信息无法做更多的事情。搜索将需要一些时间,可能会很慢,具体取决于您的正则表达式
如果您有安全问题,则显式打开文件更安全:
# 2 楼答案
在perl中:
# 3 楼答案
这里没有太多要说的,但是创建一个
BufferedReader
,一次读一行,看看它是否就是你要找的那一行# 4 楼答案
如果你有一种“为正确的工作使用正确的工具”的态度,并且可以费心学习新的工具,perl、awk甚至sed都是非常适合这种工作的工具。否则,任何完整的语言都可以,Java也可以完成这项工作。但是使用缓冲类,比如BufferedReader,否则速度会非常慢
perl中的示例:
我觉得一个班轮行得通,但有点复杂