将 Perl 变量放入 awk 命令中

Putting a Perl variable in awk command

在 Perl 中,我正在尝试在 txt 文件中搜索用户输入的字符串并打印出找到该字符串的每一行。

这是我的

my $input = <STDIN>;
print `sed -n "/$input/p" inputfile.txt`;

我收到此错误: sed -e 表达式 #1,字符 9:未终止的地址正则表达式

谁能帮我解决这个问题?

不要shell使用 sed 来做 Perl 可以为您做的事情。

my $input = <STDIN>;
chomp $input;
open my $fh, '<', 'inputfile.txt' or die $!;
while ( <$fh> ) {
    print if /$input/;
}