I am trying to remove a line from a file using grep -v or sed but the problem is the number of line in the result is still the same after removal.$ grep $'\t59116613\t' ../chr/19.TB0002183.all.vcf 19      59116613        .       T       C       20.3144 .       DP=42;VDB=0.566336;SGB=-0.651104;RPB=0.504878;MQB=0.0279668;MQSB=0.943468;BQB=0.362091;MQ0F=0;ICB=1;HOB=0.5;AC=1;AN=2;DP4=21,13,2$ grep -v $'\t59116613\t' ../chr/19.TB0002183.all.vcf | wc -l108067$ wc -l ../chr/19.TB0002183.all.vcf 108067
removing a line from a file with grep or sed but line count still the same
sed;grep
When line counting gives unexpected results, we should check if each text processing utility understands what a line is the same way. This usually boils down to two questions:1) File conversion issues: What charactes(s) are used as the EOL (end-of-line aka newline or line break)? It could be \r, \n, or \r\n depending on the OS.2) Is the very last line also terminated with the EOL? If not, how the text processing utilities handle it?In this case the second question was leading to an explanation.What does the wc -l count?Directly from the man page:-l, --lines print the newline countsObviously, the last line is not counted, if it happens to be not terminated with a newline, i.e. the result is then one less than the real number of lines.What does grep produce?grep terminates each output line with a newline. I did not find it in the documentation, only tested it.We can now make a test to explain the behaviour described in the question:Let's create a test file with 3 lines but without a trailing newlineecho -ne first\nsecond\nthird > 3lines.txtLet's check the last character:tail -1 3lines.txt | od -cOk, no '\n'.wc -l < 3lines.txtOutput is: 2 (last line not counted, because last newline is missing)Pass all lines through grepgrep . < 3lines.txt | wc -lOutput is 3 (all lines counted, because no newline is missing)Filter out just one line (it can be any line)grep -v first < 3lines.txt | wc -lOutput is 2 (again all lines counted).