LinuxQuestions.org
Visit Jeremy's Blog.
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 03-18-2013, 09:53 PM   #1
johnjlaw
LQ Newbie
 
Registered: Mar 2013
Location: New Jersey
Distribution: Mint, Ubuntu
Posts: 3

Rep: Reputation: Disabled
Linux Command Line Grep


Greetings, I just started learning the Linux command-line, I am reading a book on it and today I learned a little about pipelines, redirection and a little about grep. I tried the example out of the book and it worked as expected but as I tried an example for myself, the results were not as I had expected. In the following I tried to filter out the results of the “ls” command with “grep” to only show files containing the characters '.c',

In my folder /home/linus/prog where I keep my C programming source-files, amongst other things, I ran the following command:

$ ls | grep .c

and the results:

booksrc
booksrc.zip
char_array2.c
convert2.c
convert.c
datatype_sizes.c
firstprog.c
myrwprog.c
pointer.c
project
testc

First off I am sure there are dozens of more efficient and better ways to accomplish this simple task but I am trying to learn the basics, in particular "grep". Anyway why does booksrc,booksrc.zip, project, and testc all show up? Does grep ignore non-alphabetic characters? Or maybe '.' is some wild card I am unaware of?

Thank-you very much for the help in advance.
 
Old 03-18-2013, 10:17 PM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
Quote:
Or maybe '.' is some wild card I am unaware of?
Yes, there is. It's behaving as if the . in .c is a wildcard for "any single character", in the same manner that a ? is supposed to do.

Last edited by bigrigdriver; 03-18-2013 at 10:24 PM.
 
1 members found this post helpful.
Old 03-18-2013, 10:30 PM   #3
johnjlaw
LQ Newbie
 
Registered: Mar 2013
Location: New Jersey
Distribution: Mint, Ubuntu
Posts: 3

Original Poster
Rep: Reputation: Disabled
Hey BigRigDriver, thanks for the quick answer. I appreciate it.
 
Old 03-20-2013, 05:51 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
grep uses regular expressions for pattern matching, and in regex, a '.' has the special meaning of 'any single character'.

To get the list you want, use a bracket expression to isolate the character and treat it as a literal value, or backslash it, or use the '-F' option to force it to be treated as a literal string.

Code:
ls | grep '[.]c'
ls | grep '\.c'
ls | grep -F '.c'
Also, don't forget to always quote your expressions. This is vitally important when working with strings that can contain shell-reserved characters.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes


In any case, while the above may be good for practice, file listing and processing should almost always be done with globbing patterns instead, or find, if you need more advanced matching (and be sure to use null separators when you do).

You should never depend on parsing ls for lists of files or their metadata.

Last edited by David the H.; 03-20-2013 at 05:57 AM. Reason: fixt links
 
1 members found this post helpful.
Old 03-20-2013, 08:23 AM   #5
clocker
Member
 
Registered: Sep 2011
Distribution: fedora, redhat, mint
Posts: 87

Rep: Reputation: Disabled
use this
Quote:
ls | grep *.c
 
Old 03-20-2013, 01:36 PM   #6
johnjlaw
LQ Newbie
 
Registered: Mar 2013
Location: New Jersey
Distribution: Mint, Ubuntu
Posts: 3

Original Poster
Rep: Reputation: Disabled
David the H, clocker, thanks for the answer and especially David for the information and the links you've provided. I've bookmarked that website and will use it for further reference.
 
Old 03-26-2013, 11:39 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Except that (sorry to say) clocker's command is completely wrong, for the reasons explained before. grep does not support simple globbing patterns like that, only regex or fixed strings.

And again, you shouldn't be using ls for things like this anyway.

Other than that, good for you. Greg's wiki is perhaps the very best scripting site I've ever found, with bash-hackers.org coming in a close second.
 
  


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
[SOLVED] run ps|grep command by script/command line ... ERROR: Unsupported option (BSD syntax) masuch Programming 4 05-23-2012 04:13 AM
[SOLVED] Command to grep complete line Fracker Linux - Newbie 6 05-11-2011 04:52 AM
Want to print fouth line after resulted line using grep with tail command saurabhmehan Linux - Newbie 10 08-04-2010 11:38 PM
Grep command and adding a line Treikayan Linux - Newbie 4 07-03-2009 09:57 AM
Help me in Grep Command + cd command in single line JeiPrakash Linux - Newbie 3 05-27-2008 04:16 AM

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

All times are GMT -5. The time now is 09:36 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
Open Source Consulting | Domain Registration