Created
April 23, 2018 21:17
-
-
Save tonejito/c9c0bffd75d8c81483f9107c609439e1 to your computer and use it in GitHub Desktop.
greppy - Save grep matched and not matched lines to different files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# = ^ . ^ = | |
# greppy - Save grep matched and not matched lines to STDOUT and STDERR | |
# Andres Hernandez - tonejito | |
# | |
# https://gist.github.com/tonejito/c9c0bffd75d8c81483f9107c609439e1 | |
# | |
# This script is released under the BSD 3-clause license | |
# https://opensource.org/licenses/BSD-3-Clause | |
use strict; | |
use warnings; | |
my $HELP = " | |
Usage: $0 regex 0< STDIN 1> STDOUT 2> STDERR | |
regex Perl-compatible regular expression | |
STDIN Input file | |
STDOUT Matched lines | |
STDERR Not matched lines | |
" ; | |
die $HELP.'\n' unless ( @ARGV > 0 ); | |
while(<STDIN>) | |
{ | |
if ($_ =~ /$ARGV[0]/) | |
{ | |
print STDOUT $_; | |
} | |
else | |
{ | |
print STDERR $_; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment