LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 05-25-2005, 05:24 PM   #1
Seb74
LQ Newbie
 
Registered: Oct 2004
Posts: 24

Rep: Reputation: 15
Question Problem matching strings with grep/egrep


Hi!

Trying my new knowledge on using pipes and grep on some ls filelisting output and notice grep really works strange.

If I try ls -l | egrep -i '*te*' it doesn't just find files containing the 'te'-string, it lists almost every file in my home-folder. Just check a little sample of the large output. Doesn't make much sence.
-rw-rw-r-- 1 seb seb 138454 maj 6 16:21 alien-night.1024.jpg
-rw-rw-r-- 1 seb seb 939871 maj 8 13:49 autoconf2.5-2.59-6mdk.src.rpm
-rw-r--r-- 1 seb seb 16 maj 25 14:03 bort
drwx------ 3 seb seb 4096 maj 24 16:39 Desktop/
-rw-rw-r-- 1 seb seb 2306461 maj 8 13:45 gtk+-1.2.10-41mdk.src.rpm
drwxrwxr-x 6 seb seb 4096 maj 25 20:40 JavaApplication1/
drwxrwxr-x 6 root root 4096 maj 25 12:37 rss-kde-0.3/
-rw-rw-r-- 1 seb seb 4858642 maj 25 12:28 rss-kde-0.3.tar.bz2
-rw-r--r-- 1 seb seb 200424 maj 3 23:22 stargate.jpg

While trying ls -l | egrep -i '*test*' it just finds the files containing the 'test'-string as follows.
$ ls -l | egrep -i '*test*'
-rw-r--r-- 1 seb seb 1039 maj 25 20:17 test.class
-rw-r--r-- 1 seb seb 428 maj 25 20:17 test.java
-rw-r--r-- 1 seb seb 415 maj 25 20:15 test.java~

Removing the ' ' around makes it find nothing at all.

Exchanging * for . makes it work....although my Unix-book says * is any number of characters and . is just one character, as usually is the case, but they also use it with grep/egrep.

So, is Linux different from Unix/Solaris, or am I somehow using this wrong (even though I almost copy the syntax in my Unix course-book).

Thanks
 
Old 05-25-2005, 06:13 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Read your book again

.* is any number of any characters

But to get back to your example
ls -l *test*
will do exactly what you expect.



Cheers,
Tink
 
Old 05-25-2005, 06:18 PM   #3
Seb74
LQ Newbie
 
Registered: Oct 2004
Posts: 24

Original Poster
Rep: Reputation: 15
Nope, just '*' in the book and not '.*'

And yes, I know I dont have to use grep for filtering filenames out, I'm just trying to learn grep, and learning grep on ls-output was a good idea I thought.
Anyway the '^' and '$' works for matching start/end of the string, so ls -l | egrep -i '^-' for example finds all files and skips directorys
Cant do that with only ls sadly.

So, I guess Unix/Solaris works different from Linux then....or my Cisco-book has some errors in it
 
Old 05-25-2005, 06:24 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Problem remains ...

* is a QUANTIFIER for grep, not a QUALIFIER.

if you use
egrep -i '*te*'
the first astrix has NOTHING to have any number
of, therefore will give you EVERYTHING.

If you did
egrep -i 'te*'
it would show you any line that has a t
followed by ANY NUMBER of e's.

If you use
egrep -i '.*te.*'
it will give you the expected result.


Cheers,
Tink
 
Old 05-26-2005, 04:15 AM   #5
Seb74
LQ Newbie
 
Registered: Oct 2004
Posts: 24

Original Poster
Rep: Reputation: 15
Ah, ok, I think I understand. And I guess the book is right too then

But I dont think the first .* is needed in this example.
egrep -i '.*te.*'
Cause grep returns the row if it finds 'te' anywhere...it doesn't care about start/end of lines unless you specify with ^ or $. Right?

Well well, very nice to have this cleared up a bit...thanks a lot
I was going nuts over here trying to understand why the * sometimes worked and sometimes totally messed everything up
 
Old 05-26-2005, 02:40 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Yep - it's not needed. I just threw it in because of
your use of *te

One thing I should have mentioned in the
example above is that te* (matches t followed
by ANY number of es) also includes "all ts
followed by ZERO es", in other words, anything
with a t in it ;}


Cheers,
Tink
 
  


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
Find/grep command to find matching files, print filename, then print matching content stefanlasiewski Programming 9 06-30-2016 06:30 PM
grep multiple strings GEEXTER Linux - General 7 12-06-2013 10:56 PM
grep/egrep logical AND function? boozer_2 Linux - Newbie 11 04-10-2010 02:19 AM
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 05:04 AM
Using Grep and Egrep linux-nerd Linux - General 5 10-10-2004 12:37 PM


All times are GMT -5. The time now is 08:17 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration