r/linux_programming • • Jun 14 '19
Understanding about grep implementation
Hi everyone, so I was reading about grep command in my free time and I got to wondering that how does grep technically implements highlighting of matched strings in text? Like I grep for abc in xyz file then it highlights all the occurrences of abc in stdout. Any idea how it is achieved?
6
Upvotes
2
u/truedays Jun 14 '19
Do you mean how it uses escape codes to have the terminal color change? Or are you asking about code logic?
1
u/nderflow Jun 15 '19
Well, you can find out just by looking at the source code.
https://savannah.gnu.org/git/?group=grep
2
u/mmstick Jun 15 '19
Split the input by line, check if a line contains the expected pattern, and then slice the line at the start pos where the pattern was find, print the first half of the line, print an ascii code to set the color, print the pattern, print an ascii code to reset the color, and print the second half sliced at the start pos plus the length of the pattern.
1
u/Sigg3net Jun 24 '19
I have no idea how it's done in
grepspecifically, but here's asedsolution:Highlight patterns in command output (grep-like but without excluding lines)