I want to filter a file for lines starting with a space. I use the following command:
grep -v "^ " < input > input_no_starting_space
To double check my results, I run the following:
grep "^ " < input > double_check
and then count the number of lines in input_no_starting_space
and double_check
to see whether their sum adds up to the number of lines in input
. For this I use wc -l
.
For some reason, this check fails. Meaning, the sum of the number of lines is less than the number of lines in input
. My file has millions of lines, but I cannot seem to reproduce the issue on a small example. Is there by any chance something wrong with the way I use grep (since I would expect that grep
and grep -v
always give the complement of one another), or is this more likely an artifact in my file? In case of the latter, what could this artifact be?
This is using GNU grep 3.4 on Ubuntu 20.04.3.
diff -u <(cat input_no_starting_space double_check | sort) <(sort < input)
wc -l
, right? Does using/usr/bin/grep -v "^ "
and/usr/bin/grep "^ "
make any difference? I'm thinking you might have an alias that's adding--color=always
or something and that could be confusing things. I would also try passing the file throughdos2unix
orsed -i 's/\r//g'
, just in case, although I don't really see how the\r
would cause this specific behavior.input
in the meantime?wc -l
indeed. I tried using/bin/grep
as well as doing thesed
command before, but still the same behaviorgrep
commands. I also check the number of lines both before and after executing thegrep