mad*_*per 1 linux shell perl awk
我只想在我的perl脚本中使用awk命令,如下所示:
$linum = `awk -F "%" '/^\s*kernel/{print NR}' < $grubFile`;
Run Code Online (Sandbox Code Playgroud)
但它会说:Unrecognized escape \s passed through at ./root line 36.
我该如何避免呢?谢谢.
$x = `... \s ...`;
Run Code Online (Sandbox Code Playgroud)
没有比这更有意义了
$x = "... \s ...";
Run Code Online (Sandbox Code Playgroud)
如果你想两个字符\和s,你需要逃避\双引号的文字和类似.就像你使用的一样
$x = "... \\s ...";
Run Code Online (Sandbox Code Playgroud)
你需要使用
$x = `... \\s ...`;
Run Code Online (Sandbox Code Playgroud)
请注意,您完全无法正确转义内容$grubFile.如果文件名包含空格,则命令将失败.并考虑如果它包含其他特殊的shell特征,可能会发生什么|.
正如@ysth所示,以下内容相当于您的命令:
awk -F% '/^\s*kernel/{print NR}' "$grubFile"
Run Code Online (Sandbox Code Playgroud)
摆脱输入重定向意味着你可以简单地使用
use IPC::System::Simple qw( capturex );
my @line_nums = capturex('awk', '-F%', '/^\s*kernel/{print NR}', $grubFile);
chomp @line_nums;
Run Code Online (Sandbox Code Playgroud)
顺便说一下,用Perl纯粹做它并不难.
my @line_nums;
open(my $fh, '<', $grubFile) or die $!;
while (<$fh>) {
push @line_nums, $. if /^\s*kernel/;
}
Run Code Online (Sandbox Code Playgroud)