添加 ||正则表达式到 perl 脚本中的 bash ``ed one-liner

发布于 2024-12-09 21:38:20 字数 720 浏览 2 评论 0原文

我正在尝试添加 ||正则表达式到 perl 脚本中的 bash ``ed 一行,如果这有意义的话。

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 或 none 开头的行,但如果可能的话,我还想将“或包含 .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";
}

I am trying to add an || regex to a bash ``ed one-liner in a perl script, if that makes any sense.

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";

So I am removing lines from the output that start with Filesystem, tmpfs, cdrom or none, at present, but I would also like to and the "or lines containing .Private" to the one-liner, if possible...

I have the below also, but want to reproduce its results with the above code...

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";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌路终见情 2024-12-16 21:38:20

我建议您完全摆脱“awk”部分。从 Perl 内部调用 awk 是愚蠢的。

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

<代码>
我的@lines = df -H;

我的@results = grep ... @lines; # perl 'grep' 内置

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

I'd recommend that you get rid of the "awk" part entirely. Calling awk from inside perl is silly.

Instead, rely on capturing lines using list context, and then do your processing inside perl.


my @lines = df -H;

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

If you insist on using the unix grep, why not just add '|.Private' to your grep exclusion pattern?

撩起发的微风 2024-12-16 21:38:20

您只需将 \.Private 部分添加到当前的正则表达式中:

grep -vE '^Filesystem|tmpfs|cdrom|none|\.Private'

顺便说一句,模式 ^Filesystem|tmpfs|cdrom|none 实际上可能不会执行您的操作因为只有 Filesystem 在行的开头匹配,所以如果其他部分出现在输入中的任何位置,它们也会被匹配。要在开头匹配它们,请将其更改为:

'^Filesystem|^tmpfs|^cdrom|^none'

You just need to add the \.Private part to the current regexp:

grep -vE '^Filesystem|tmpfs|cdrom|none|\.Private'

On a side note, the pattern ^Filesystem|tmpfs|cdrom|none might not actually do what you want, as only Filesystem is matched at the beginning of the line, the other parts will be matched if they appear anywhere in the input. To match them at the beginning, change it to:

'^Filesystem|^tmpfs|^cdrom|^none'
謸气贵蔟 2024-12-16 21:38:20

你的正则表达式并没有按照你的想法去做。它匹配以 Fileystem 开头或在任何位置包含其他单词的字符串。

尝试用这个:

grep -vE '^(Filesystem|tmpfs|cdrom|none)|\.Private'

Your regex doesn't do what you think it does. It matches strings that start with Fileystem or that contain the other words anywhere.

Try with this:

grep -vE '^(Filesystem|tmpfs|cdrom|none)|\.Private'
葵雨 2024-12-16 21:38:20

像这样?

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

Like this?

my $result = `df -H | grep -vE '(^Filesystem|tmpfs|cdrom|none)|\.Private'  | awk '{ print \$1 "\t" \$5 " used."}'`; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文