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)
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
I fear this may not be possible. I'd use Perl and do something like:
if (/^AAA/) {
print STDOUT $_;
}
else
{
print STDERR $_;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With