Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep redirect non-matching

I'm doing a simple grep for lines starting with some patteren like:

grep -E "^AAA" myfile > newfile

I would like to also (in the same go) redirect those non-matching lines to another file.
I know it would be possible to simply do it twice and use -v in the second try, but the files are (relatively) huge and only reading them once would save some quite valuable time...

I was thinking something along the line of redirecting non-matching to stderr like:

grep -E -magic_switch "^AAA" myfile > newfile 2> newfile.nonmatch

Is this trick somehow possible with grep or should I rather just code it?

(might be of additional value - I'm coding this in bash script)

like image 405
nEJC Avatar asked Dec 16 '22 18:12

nEJC


2 Answers

This will work:

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

or

awk -v matchfile=/path/to/file1 -v nomatchfile=/path/to/file2 '/pattern/ {print > matchfile; next} {print > nomatchfile}' inputfile

or

#!/usr/bin/awk -f
BEGIN {
    pattern     = ARGV[1]
    matchfile   = ARGV[2]
    nomatchfile = ARGV[3]
    for (i=1; i<=3; i++) delete ARGV[i]
}

$0 ~ pattern {
    print > matchfile
    next
}

{
    print > nomatchfile
}

Call the last one like this:

./script.awk regex outputfile1 outputfile2 inputfile
like image 106
Dennis Williamson Avatar answered Dec 30 '22 16:12

Dennis Williamson


I fear this may not be possible. I'd use Perl and do something like:

if (/^AAA/) {
   print STDOUT $_;
}
else
{
   print STDERR $_;
}
like image 20
Brian Agnew Avatar answered Dec 30 '22 16:12

Brian Agnew