The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
UNIX newbie NEWBIE question! Hanamachi UNIX for Dummies Questions & Answers 4 03-28-2009 04:10 PM
Newbie needs help with simple script shizizzle Shell Programming and Scripting 12 03-21-2006 12:01 PM
Ok simple question for simple knowledge... Corrail UNIX for Dummies Questions & Answers 1 11-28-2005 01:03 PM
Simple grep question, but I'm out of practice citygov Shell Programming and Scripting 0 08-02-2005 10:31 AM
simple grep question google UNIX for Dummies Questions & Answers 5 01-27-2003 11:00 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-05-2008
doubleminus doubleminus is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 27
Simple newbie grep question

How come grep [sd] testfile1 won't find anything in testfile1 (even though the characters sd are there in great quantity), but grep '[sd]' testfile1 will find plenty?

Do the single quotes prevent the shell from interpreting the [ test command? So when not using single quotes, the grep [sd] testfile1 is interpreted as: grep *test whether or not characters sd exist* testfile1?

Or is the explanation much simpler? Thanks!
  #2 (permalink)  
Old 04-05-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
You are correct - you have to prevent the shell from interpreting the the '[sd]' regex.
  #3 (permalink)  
Old 04-05-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Just for completeness, [sd] without the quotes is actually a shell wildcard, not a "test" invocation. It matches any file whose name is s or d.
  #4 (permalink)  
Old 04-05-2008
doubleminus doubleminus is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 27
Thank you!

Second question: Is there way to insert math into a grep, for example:

In a large chat log file I want to grep for every instance of an abbreviation of an ordinal number: 21st, 31st, 41st, 101st, 401st, 2nd, 22nd, 32nd etc.

So I would want to do egrep [x+10]st|y+10]nd|[z+10]rd] chatlog.txt or something like that. Is there any way to have the shell evaluate this math and append 'st' 'nd' 'rd' as necessary? Thanks!

Last edited by doubleminus; 04-05-2008 at 04:11 PM..
  #5 (permalink)  
Old 04-06-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Read up on regular expressions. The egrep manual page has a very brief summary; some systems also have a regex(5) manual page which is a bit more detailed. Any good Unix beginners book will have a chapter or five on the subject.

There's absolutely no math in there but it's a very powerful and versatile facility for matching ... stuff.

Code:
egrep '1st|2nd|3rd|[4-9]th' chatlog.txt
Note that this will also match '21st', '103245th', etc. The grep family of tools are strictly substring-based and line-oriented; any line containing a match anywhere in it will be printed.
  #6 (permalink)  
Old 04-06-2008
doubleminus doubleminus is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 27
Great explanation. It's much more clear now. I was just making things a bit too complicated. Thank you for the help.
Closed Thread

Bookmarks

Tags
regex, regular expressions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:17 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0