Issue
I'm trying to mimic grep
One thing I don't understand is: when should I print a line separator --
?
For example, running the command:
cat test.txt | grep -b -v -A 1 -i on
(I'll paste text.txt at the bottom)
I get the following output:
87:aaa
91:
92:from the start Her daughters were identical twins but the resemblance was purely physical
185-Madeline had been difficult baby from day one She was one who cried nonstop who
269:refused to nurse When Paige would finally get her settled and carefully place her down
358:she rarely got as far as the nursery door before Madelines blood curdling cries began
444-again often waking Erica as well Paige would want to sob on the floor she was so
529:absolutely depleted
550:Madeline was an infant who made Paige understand why Shaken Baby Syndrome was thing
It makes perfect sense in terms of matching lines and trailing context (because -A) but I thought a line separator should be printed between continous blocks of printed lines:
For example, for the same input, my program prints:
87:aaa
91:
92:from the start Her daughters were identical twins but the resemblance was purely physical
185-Madeline had been difficult baby from day one She was one who cried nonstop who
--
269:refused to nurse When Paige would finally get her settled and carefully place her down
358:she rarely got as far as the nursery door before Madelines blood curdling cries began
444-again often waking Erica as well Paige would want to sob on the floor she was so
--
529:absolutely depleted
550:Madeline was an infant who made Paige understand why Shaken Baby Syndrome was thing
My reasoning - on the output itself:
87:aaa -- no match, so print (because -v)
91: -- no match so print (because -v)
92:from the start Her daughters were identical twins but the resemblance was purely physical -- no match, so print (because -v)
185-Madeline had been difficult baby from day one She was one who cried nonstop who -- match, print because -A 1 and -v -> extra line - last extra line , so must print a line seperator if found a new match
-- -> print a line separator - found a new line that has no match (because -v )
269:refused to nurse When Paige would finally get her settled and carefully place her do
..
.
.
The text.txt:
Their mother Paige would have told you there was something wrong with Madeline right
aaa
from the start Her daughters were identical twins but the resemblance was purely physical
Madeline had been difficult baby from day one She was one who cried nonstop who
refused to nurse When Paige would finally get her settled and carefully place her down
she rarely got as far as the nursery door before Madelines blood curdling cries began
again often waking Erica as well Paige would want to sob on the floor she was so
absolutely depleted
Madeline was an infant who made Paige understand why Shaken Baby Syndrome was thing
So my question is: when exactly does grep prints a line separator ?
Solution
Note: I'm not answering about -v
and context matching, since I don't think they are well defined or even supposed to work. For example, this output doesn't make sense:
$ seq 5 | grep -v -A1 '3'
1
2
3
4
5
The separator --
won't be added if two or more groups of matching lines have overlapping lines or are next to each other in input file. Consider this sample input:
$ cat context.txt
wheat
roti
bread
blue
toy
flower
sand stone
light blue
flower
sky
water
dark red
ruby
blood
evening sky
Case 1: groups are next to each other
$ grep -C1 'flower' context.txt
toy
flower
sand stone
light blue
flower
sky
Case 2: overlapping groups
$ grep -A4 'flower' context.txt
flower
sand stone
light blue
flower
sky
water
dark red
ruby
Answered By - Sundeep Answer Checked By - Cary Denson (WPSolving Admin)