Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex perl一行程序+;如何过滤文件_Regex_Linux_Bash_Perl - Fatal编程技术网

Regex perl一行程序+;如何过滤文件

Regex perl一行程序+;如何过滤文件,regex,linux,bash,perl,Regex,Linux,Bash,Perl,背景:我的目标是过滤包含单词-old 我想打印除包含旧单词(大写或小写字母)的文件以外的所有文件 根据以下规则: 如果Az zZ位于旧名称之前或之后,则应打印第行 如果Az Zz位于旧名称之后,旧单词之前,则应打印行 如果0-9位于旧名称之后,旧单词之前,则应打印第行 如果0-9位于旧名称之前,旧名称之后为非Az zZ或0-9,则不应打印行 如果0-9位于旧名称之后,旧名称之前为非Az zZ或0-9,则不应打印行 例子 /DIR3/DATA/A4/Via/OOld/TriR.txt

背景:我的目标是过滤包含单词-old

我想打印除包含旧单词(大写或小写字母)的文件以外的所有文件

根据以下规则:

如果Az zZ位于旧名称之前或之后,则应打印第行

如果Az Zz位于旧名称之后,旧单词之前,则应打印行

如果0-9位于旧名称之后,旧单词之前,则应打印第行

如果0-9位于旧名称之前,旧名称之后为非Az zZ或0-9,则不应打印行

如果0-9位于旧名称之后,旧名称之前为非Az zZ或0-9,则不应打印行

例子

  /DIR3/DATA/A4/Via/OOld/TriR.txt            --> should  be print
  /DIR4/DATA/A4/Via/AOld1/Comne.txt          --> should be print
  /DIR5/DATA/A4/Via/BOld/TriR.txt     --> should be print
  /DIR5/DATA/A4/Via/aOld/TriR.txt      --> should be print
  /DIR5/DATA/A4/Via/1OldA/TriR.txt      --> should be print
  /DIR5/DATA/A4/Via/POld1/TriR.txt      --> should be print
  /DIR4/DATA/A4/Via/1Old1/Comne.txt    --> should  be print
  /DIR4/DATA/A4/Via/1Old1/Comne.txt    --> should  be print
  /DIR4/DATA/A4/Via/Comne.txt    --> should  be print


  /DIR1/DATA/A4/Via/5Old/CentalS.txt   --> should not be print
  /DIR4/DATA/A4/Via/Old1/Comne.txt    --> should not be print
  /DIR1/DATA/A4/Via/Old/CentalS.txt   --> should not be print
  /DIR4/DATA/A4/Via/Old11/Comne.txt    --> should not be print
  /DIR4/DATA/A4/Via/OLD@/Comne.txt    --> should not be print
  /DIR4/DATA/A4/Via/.OLd/Comne.txt    --> should not be print
  /DIR4/DATA/A4/Via/home/Comne.Old_txt    --> should not be print
  /DIR4/DATA/A4/Via/home/Comne.old_txt    --> should not be print
  /DIR4/DATA/A4/Via/home/Comne.0old_txt    --> should not be print
  /DIR4/DATA/A4/Via/home/Comne.old6_txt    --> should not be print
  /DIR4/DATA/A4/Via/home/Comne___0old_txt    --> should not be print
请建议如何通过perl一行代码实现这一点 语法


如果文件路径位于单个文件中,则可以写入

perl -ne'$w='old';print if /[a-z]$w|$w[a-z]|[0-9]$w[0-9]/i' myfile
或者,如果你愿意的话

perl -ne'print if /(.)old(.)/i && "$1$2" =~ /[a-z]|[0-9]{2}/i' myfile
实际上,看起来您想要过滤掉
PATH
环境变量的成员,所以您需要

perl -E'say for grep /(.)old(.)/i && "$1$2" =~ /[a-z]|[0-9]{2}/i, split /:/, $ENV{PATH}'

更新

我误解了你的问题。这应该对你有用

perl -lnE'/(.)old(.)/i && "$1$2" !~ /[a-z]|[0-9]{2}/i or say for /[^:]+/g'

我不理解您对管道
PATH
和指定给定路径的要求,因此这两种方法都适用。您也可以在命令

之后放置一个输入文件,而不是
echo$PATH |
,让我们使用
echo“${PATH/://$'\n'}”|
(find),它逐行打印路径。您可以在这一行上标记:

perl -ne 'if($_=~m/^(?!.*old).*|^.*?(?:[a-z]old|\dold[a-z\d]).*/i){print "$&\n";}'
可以缩短为

perl -ne 'print if m/^(?!.*old).*|^.*?(?:[a-z]old|\dold[a-z\d]).*/i'
选项2

此选项应直接与
echo$PATH

perl -ne 'while(/(?:^|\G:\K)(?:(?!.*old)[^:]+|[^:]*?(?:[a-z]old|\dold[a-z\d])[^:]*)/ig){print "$&\n";}'

echo/word | perl-ne'$w='old';打印如果/[a-z]$w |$w[a-z]|[0-9]$w[0-9]/i'(为什么不打印/word?)编辑我的问题/DIR4/DATA/A4/Via/Comne.txt-->应该打印请参见我的示例中的情况(/DIR4/DATA/A4/Via/Comne.txt-->应该打印)您能解释一下“/DIR4/DATA/A4/Via/Comne.txt`我不明白打印这个问题的规则吗(没有
old
)是的,大多数文件都没有“old”,在这种情况下需要打印路径,好的。给我一分钟。:@zx81为什么在答案中有
while
循环?
-n
选项自动创建循环。
perl -ne 'while(/(?:^|\G:\K)(?:(?!.*old)[^:]+|[^:]*?(?:[a-z]old|\dold[a-z\d])[^:]*)/ig){print "$&\n";}'