Skip to main content
13 events
when toggle format what by license comment
Aug 18 at 11:19 comment added cas Since you've been given the task of implementing grep, you'll need to understand regexes themselves, and whichever library you plan to use that provides regex capabilities (unless part of that task is to implement the regex functions yourself). Even if you're required to implement everything from scratch yourself, studying existing libraries/implementations to figure out how they work and how they solved the problems you're going to run into will be useful.
Aug 18 at 11:15 comment added cas @Mark a regular expression library provides the raw functionality - that's its purpose, to provide regex capability for other programs to use. A program, such as grep, which uses that library may (and almost certainly will) expand on what the library does, like providing convenience features for users. grep, for example can show all matches on a line (-0), count the matches in a file (-c), show the line number of matches (-n), and more - none of these features are mentioned in the docs for the regex library, because they don't belong there. they belong in the docs for grep.
Aug 18 at 8:24 comment added Mark @ilkkachu I was learning regular expressions and needed a utility to practice and test my assumptions. I chose grep just because it works with regular expressions(It was worth reading carefully first what it is needed for). I expected that if the regular expression matches the first word, grep should return the same. But I misunderstood how grep works. And now I think that grep -o is not bad at all. It prints all the matched parts of the matched string on separate lines. So I just needed to look at the first line :) Maybe this is a useless question now, but the answer was good. So let it be:)
Aug 18 at 8:22 comment added Mark @cas I was given a task to implement grep, so before I started I wanted to understand what a regular expression is and how it works.
Aug 2 at 8:21 review Close votes
Aug 7 at 3:07
Aug 2 at 8:03 comment added ilkkachu As for grep -o, I think it's defined to print all matches, so again, there's no conflict between what you saw and what is documented. Grep isn't the same thing as the regex engine in the C library, and it's not reasonable to expect a description of the library functions would describe accurately the behavior of grep.
Aug 2 at 8:00 comment added ilkkachu So, why does it matter if regular grep finds the first possible match on the line, or one of the possible other matches? The default operation is defined to just print the line, regardless of what exactly matched. On the ither hand, is there reason to doubt the internal matching logic of grep wouldn't find the first match, as usual? That would mean grep would need to have a different regex engine from everything else, and for no real use, since it prints just the whole line anyway.
Aug 2 at 4:05 comment added cas Is this just for self-education about grep or do you want to actually use the extracted matches in a script? if the former, then self-education is great. If the latter, then you'll be much better off using a language like awk or perl which are designed for exactly that kind of task, trying to do it in shell with grep and command substitution will be slow and awkward. e.g. perl can return ALL matches in an array, making it easy to iterate over the results rather than repeating the search multiple times.
Aug 1 at 21:05 history became hot network question
Aug 1 at 14:27 vote accept Mark
Aug 1 at 13:36 answer added Stéphane Chazelas timeline score: 17
S Aug 1 at 12:59 review First questions
Aug 1 at 13:11
S Aug 1 at 12:59 history asked Mark CC BY-SA 4.0