gpt4 book ai didi

linux - 添加 ||正则表达式到 bash `` ed perl 脚本中的一行

转载 作者:太空宇宙 更新时间:2023-11-04 09:53:57 26 4
gpt4 key购买 nike

我正在尝试添加 ||在 perl 脚本中将正则表达式转换为 bash ``ed one-liner,如果这有意义的话。

my $result = `df -H | grep -vE '^Filesystem|tmpfs|cdrom|none'  | awk '{ print \$1 "\t" \$5 " used."}'`; 

# .Private maybe the same as /dev/sdb1 so I'm trying to remove it too
# by trying to add || (m/\.Private/) to the above

print "$result";

因此,我目前正在从输出中删除以 Filesystem、tmpfs、cdrom 或无开头的行,但如果可能的话,我也想将“或包含 .Private 的行”添加到一行中。 .

我也有下面的,但想用上面的代码重现它的结果......

my @result2 =Shell::df ("-H"); 
shift @result2; # get rid of "Filesystem..."
for( @result2 ){
next if ((/^tmpfs|tmpfs|cdrom|none/) || (m/\.Private/));
my @words2 = split('\s+', $_);
print $words2[0], "\t", $words2[4], " used\.\n";
}

最佳答案

我建议您完全去掉“awk”部分。从 perl 内部调用 awk 很愚蠢。

相反,依靠使用列表上下文捕获行,然后在 perl 中进行处理。


my @lines = <code>df -H</code>;



my @results = grep ... @lines; # perl 'grep' builtin

如果您坚持使用 unix grep,为什么不将“|.Private”添加到您的 grep 排除模式中呢?

关于linux - 添加 ||正则表达式到 bash `` ed perl 脚本中的一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7752939/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com