Problems with grep -E and regular expressions
Hey there!
I'm actually working myself through a tutorial on http://linuxzoo.net/ wich is quite nice for learning some basic linux commands. I'm a little stuck in tutorial 5 where it says: Quote:
Code:
find /var/ -user root | grep -E '(\/(.*?\/)+)' >s8 What confuses me here is that when I check my regular expression on a testing website like http://regexpal.com/ it clearly shows that the last slash is part of the match. But when I execute the above line, the file s8 contains all the paths without the last slash. What is even more confusing is the next task in the row: Question 12: find .conf files Quote:
Code:
find /etc/ -type f -name "*.conf" | grep -E '(\/(.*?\/)+)' >s9 How is this even possible? I'm using the same regular expression! I'm clueless. Any help appreciated. Best regards, MisterIX. |
try this, with colored output (use grep --color if not set by default)
find /var/ -user root | grep -E '(\/(.*?\/)+)' You will see the matched string contains both the first and last slash, but the whole line was printed not only the matched text. If you need only that part use -o flag too. The same is valid for the next question too |
As pan64 has answered the question and even though you also mentioned it is a section on pipelining, just thought I would check that you are aware find itself could have produced the desired output :)
Code:
find /var/ -user root -printf "%h\n" 2>/dev/null |
Excellent, thank you pan64 I really missed out on the -o parameter. I get it now: a match is a match is a match ;) .
And thank you, too grail, your solution is very elegant. No matter the testing system didn't count either solution as solved, but hey at least I learned something! |
you are welcome
if you really want to say thanks just click on yes |
All times are GMT -5. The time now is 11:47 PM. |