LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-23-2015, 06:01 AM   #1
MisterIX
LQ Newbie
 
Registered: Sep 2015
Posts: 5

Rep: Reputation: Disabled
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.
 
Old 09-23-2015, 08:55 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,790

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
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
 
Old 09-23-2015, 12:19 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
Old 09-24-2015, 03:50 AM   #4
MisterIX
LQ Newbie
 
Registered: Sep 2015
Posts: 5

Original Poster
Rep: Reputation: Disabled
Cool

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!
 
Old 09-24-2015, 03:55 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,790

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
you are welcome
if you really want to say thanks just click on yes
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular expressions not working for grep?? sethbw Linux - Newbie 5 10-02-2012 02:44 PM
[SOLVED] Grep regular expressions rahularjun86 Linux - Newbie 5 12-03-2011 04:41 PM
[SOLVED] Regular Expressions and Grep metallica1973 Linux - General 8 03-08-2011 08:17 AM
[SOLVED] Help using wc and grep with regular expressions citygrid Linux - Newbie 12 04-17-2010 02:29 AM
Regular expressions using grep linuxmandrake Programming 3 11-16-2005 04:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration