LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Problems with grep -E and regular expressions (https://www.linuxquestions.org/questions/linux-newbie-8/problems-with-grep-e-and-regular-expressions-4175554280/)

MisterIX 09-23-2015 06:01 AM

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:

Question 11: find root files

Find all the files in /var (including subdirectories) that are owned by user root. Send the list of full path names to s8.

Your find command may produce "Permission Denied" errors during this search. This will have no effect on the answer, and this error can be safely ignored for this question.
This is a pipelining exercise so I did this:

Code:

find /var/ -user root | grep -E '(\/(.*?\/)+)' >s8
I use grep -E with a regular expression that should only extract the path from found files and write it in a file called 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:

Find all the files in /etc (including subdirectories) end .conf Send the list of full path names to s9.

Your find command may produce "Permission Denied" errors during this search. This will have no effect on the answer, and this error can be safely ignored for this question.
So here I did this:

Code:

find /etc/ -type f -name "*.conf" | grep -E '(\/(.*?\/)+)' >s9
Which conludes in a file s9 where not only the path is visible but also all the names of the .conf files.

How is this even possible? I'm using the same regular expression!

I'm clueless. Any help appreciated. Best regards, MisterIX.

pan64 09-23-2015 08:55 AM

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

grail 09-23-2015 12:19 PM

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
In case you haven't covered redirection of different streams, then end part simply sends stderr to /dev/null to get rid of the "Permission Denied" errors :)

MisterIX 09-24-2015 03:50 AM

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!

pan64 09-24-2015 03:55 AM

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.