Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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
Bash grep重定向不匹配_Bash_File_Command Line_Grep - Fatal编程技术网

Bash grep重定向不匹配

Bash grep重定向不匹配,bash,file,command-line,grep,Bash,File,Command Line,Grep,我正在做一个简单的grep以一些模式开头的行,如: grep -E "^AAA" myfile > newfile 我还想(以同样的方式)将那些不匹配的行重定向到另一个文件。 我知道只需做两次并在第二次尝试中使用-v是可能的,但文件(相对)很大,只读取一次将节省一些非常宝贵的时间 我在考虑将不匹配重定向到stderr,比如: grep -E -magic_switch "^AAA" myfile > newfile 2> newfile.nonmatch 这一技巧在grep

我正在做一个简单的grep以一些模式开头的行,如:

grep -E "^AAA" myfile > newfile
我还想(以同样的方式)将那些不匹配的行重定向到另一个文件。
我知道只需做两次并在第二次尝试中使用-v是可能的,但文件(相对)很大,只读取一次将节省一些非常宝贵的时间

我在考虑将不匹配重定向到stderr,比如:

grep -E -magic_switch "^AAA" myfile > newfile 2> newfile.nonmatch
这一技巧在grep中是否可能实现,或者我应该直接编写代码吗


(可能有附加价值-我正在用bash脚本编写)

我担心这可能不可能。我将使用Perl并执行以下操作:

if (/^AAA/) {
   print STDOUT $_;
}
else
{
   print STDERR $_;
}

我不相信用
grep
可以做到这一点,但这只是Perl的几行代码:

#! /usr/bin/perl
# usage: script regexp match_file nomatch_file < input

my $regexp = shift;
open(MATCH, ">".shift);
open(NOMATCH, ">".shift);

while(<STDIN>) {
    if (/$regexp/o) {
        print MATCH $_;
    } else {
        print NOMATCH $_;
    }
}
#/usr/bin/perl
#用法:脚本regexp match_file nomatch_file”.shift);
打开(NOMATCH,“>”.shift);
while(){
如果(/$regexp/o){
打印匹配$;
}否则{
打印NOMATCH$;
}
}
或者Python,如果您愿意:

#! /usr/bin/python
# usage: script regexp match_file nomatch_file < input

import sys
import re

exp = re.compile(sys.argv[1])
match = open(sys.argv[2], "w")
nomatch = open(sys.argv[3], "w")

for line in sys.stdin:
    if exp.match(line): match.write(line)
    else:               nomatch.write(line)
#/usr/bin/python
#用法:脚本regexp match_file nomatch_file
(两者都未经测试。您的里程数可能有所不同。禁止时无效。)

这将起作用:

awk '/pattern/ {print; next} {print > "/dev/stderr"}' inputfile


这里有一个函数供您使用:

function perg {
  awk '{y = $0~z ? "out" : "err"; print > "/dev/std" y}' z="$1" "$2"
}
将其与文件一起使用

perg ^AAA myfile > newfile 2> newfile.nonmatch
或者从管子里

cat myfile | perg ^AAA > newfile 2> newfile.nonmatch

在读取文件时,可以使用进程替换来复制管道(灵感)。这应该是几乎相同的性能,因为文件仍然只被读取一次

像这样的方法应该会奏效:

cat file.txt | tee>(grep'pattern'>matches.txt)| grep-v'pattern'>non-matches.txt

祝你快乐!我只需要py代码。。。您刚刚为我节省了5分钟的浏览时间;)美好的与编写代码相比,我更喜欢使用标准工具,一行程序值得额外加分;)
function perg {
  awk '{y = $0~z ? "out" : "err"; print > "/dev/std" y}' z="$1" "$2"
}
perg ^AAA myfile > newfile 2> newfile.nonmatch
cat myfile | perg ^AAA > newfile 2> newfile.nonmatch