Simple newbie grep question


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Simple newbie grep question
# 1  
Old 04-05-2008
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  
Old 04-05-2008
You are correct - you have to prevent the shell from interpreting the the '[sd]' regex.
# 3  
Old 04-05-2008
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  
Old 04-05-2008
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 05:11 PM..
# 5  
Old 04-06-2008
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  
Old 04-06-2008
Great explanation. It's much more clear now. I was just making things a bit too complicated. Thank you for the help.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Syslog.conf: looking for a simple answer on a simple question

Cheers! In /etc/syslog.conf, if an error type is not specified, is it logged anywhere (most preferable is it logged to /var/log/messages) or not? To be more precise I am interested in error and critical level messages. At default these errors are not specified in syslog.conf, and I need to... (6 Replies)
Discussion started by: dr1zzt3r
6 Replies

2. UNIX for Dummies Questions & Answers

Need help with simple Linux commands - NEWBIE here!

Hey guys, I need help with simple unix commands. I'm a newbie to Unix and don't know these commands. Any help is appreciated. 1. Logon to Linux. 2. Create a directory "Unix" under your home directory. Command(s): …………………………………………. 3. Create four... (1 Reply)
Discussion started by: loverangerguy
1 Replies

3. Shell Programming and Scripting

simple problem for a newbie

I am trying to find a way to display just sudoers, passwd and shadow files without the extensions.. Thank you for any suggestions ls -lrt /etc | egrep 'sudoers|passwd|group|shadow' -r-------- 1 root root 1338 Oct 31 2006 shadow.sav -r-------- 1 root root 5191... (4 Replies)
Discussion started by: delphys
4 Replies

4. UNIX for Dummies Questions & Answers

Simple grep question

I hope someone can help me. I have a folder e.g. /opt/application Under that are many sub folders e.g. Folder1 Folder2 Folder3 Folder4 Folder5 Folder6 etc In some of these fodlers (not all of them) is a file called errors.log I need to run a grep that will start at... (3 Replies)
Discussion started by: gunnahafta
3 Replies

5. UNIX for Dummies Questions & Answers

Simple grep question

This should be so easy... I want to find all the apps in /Applications that start with the lower case i (e.g. iTunes.app, iSync.app, iCal.app) They should all have the .app extension. I've tried: ls /Applications |grep -o i*.app ls /Applications/i*.app Anyhow, I just want to see what apps... (2 Replies)
Discussion started by: glev2005
2 Replies

6. UNIX for Dummies Questions & Answers

UNIX newbie NEWBIE question!

Hello everyone, Just started UNIX today! In our school we use solaris. I just want to know how do I setup Solaris 10 not the GUI one, the one where you have to type the commands like ECHO, ls, pwd, etc... I have windows xp and I also have vmware. I hope I am not missing anything! :p (4 Replies)
Discussion started by: Hanamachi
4 Replies

7. Shell Programming and Scripting

Simple grep Question

I tried searching for answers but didn't find any. When I grep a file results read 4.2.2.2 4.4.4.2 4.5.6.7 But I just want to select each result individually. For Example I want to be able to say variable1="first grep result" variable2="second grep result" variable3="third grep... (8 Replies)
Discussion started by: elbombillo
8 Replies

8. Shell Programming and Scripting

Newbie needs help with simple script

Hi, I'm just new here, looks like there is alot to read. I need some help on a simple script that i would like to make, and I honestly have zero experience with shell scripts or anything, so i have no idea what I am doing. I want to have a script that I can upload to to my webhosting... (12 Replies)
Discussion started by: shizizzle
12 Replies

9. Shell Programming and Scripting

Simple grep question, but I'm out of practice

Never mind, I did more research, and now am using grep -v './temp/', dumping it into a new text file, then using mv -f to make that the original file. Thanks for reading! --------------- Hi folks, I haven't done any scripting in years, and now I have a problem. Our backup tapes are filling... (0 Replies)
Discussion started by: citygov
0 Replies

10. UNIX for Dummies Questions & Answers

simple grep question

I have seen this used several times but not really sure of what it actually does. I am confused with the second grep as the argument to the first. some commands | grep -v grep | some other commands Can anyone provide an explanation? Thanks, (5 Replies)
Discussion started by: google
5 Replies
Login or Register to Ask a Question