Linux 将| |正则表达式添加到perl脚本中的bash``ed一行程序中
我试图在perl脚本中的bash``ed one行程序中添加一个| | regex,如果这有意义的话Linux 将| |正则表达式添加到perl脚本中的bash``ed一行程序中,linux,perl,bash,Linux,Perl,Bash,我试图在perl脚本中的bash``ed one行程序中添加一个| | regex,如果这有意义的话 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 ||
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开头的行,但如果可能的话,我也希望将“or line containing.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";
}
你的正则表达式并不像你想象的那样。它匹配以
filesystem
开头的字符串或在任何位置包含其他单词的字符串
试试这个:
grep -vE '^(Filesystem|tmpfs|cdrom|none)|\.Private'
您只需将
\.Private
部分添加到当前regexp:
grep -vE '^Filesystem|tmpfs|cdrom|none|\.Private'
另一方面,模式^Filesystem | tmpfs | cdrom | none
可能不会真正执行您想要的操作,因为只有文件系统
在行首匹配,如果其他部分出现在输入中的任何位置,则会匹配。要在开始时匹配它们,请将其更改为:
'^Filesystem|^tmpfs|^cdrom|^none'
像这样
my $result = `df -H | grep -vE '(^Filesystem|tmpfs|cdrom|none)|\.Private' | awk '{ print \$1 "\t" \$5 " used."}'`;
我建议你完全去掉“awk”部分。从perl内部调用awk是愚蠢的 相反,依赖于使用列表上下文捕获行,然后在perl中进行处理
my@lines=df-H
我的@results=grep@线条perl“grep”内置
如果您坚持使用unix grep,为什么不在grep排除模式中添加“|.Private”?这家伙可能正在学习和练习,他的问题没有问题。我知道这已经4年了,但值得一提。