Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 03-28-2010, 12:45 PM   #1
LQ Newbie
Registered: Mar 2010
Posts: 7

Rep: Reputation: 0
Exclamation Using grep and expressions

How can I use grep expressions so that i can find strings like the ones bellow:

New York, NY
Virginia, VA
Oregon, OR

As you can see each city starts with capital letter then it follows up with a comma, and then with the Abbreviation of the city.

I used :

grep -w '[A-Z].*[a-z][,][' '][A-Z][A-Z]' filename
But i get no results.. pls help meeee

Last edited by LordJohn; 03-28-2010 at 12:47 PM. Reason: spell checking
Old 03-28-2010, 01:41 PM   #2
Registered: Sep 2004
Location: UK
Distribution: Ubuntu
Posts: 117

Rep: Reputation: 16
grep -w '[A-Z][a-zA-Z]*[,][ ]*[A-Z][A-Z]' <FILENAME> works for me.

I'm no regex expert so it could probably be refined a bit.

If you want to match one uppercase letter, and the rest of the city name must be lowercase:
grep -w '[A-Z].[a-z]*[,][ ]*[A-Z][A-Z]' <FILENAME> seems to work

Last edited by clb; 03-28-2010 at 01:42 PM. Reason: Added second regex
Old 03-28-2010, 04:45 PM   #3
LQ Newbie
Registered: Mar 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Really thx
You are a life saver... :P


egrep, grep, programming

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 On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
using grep with expressions ANDed logicalfuzz Linux - Newbie 20 11-16-2010 12:17 AM
Grep Expressions? Ordinary12 Linux - General 3 10-13-2009 11:19 PM
[SOLVED] Why this grep command with regular expressions not working on my system? Andrew Dufresne Linux - Newbie 12 10-01-2009 02:38 PM
Commands and Common Expressions-ls/grep rtrahan Linux - Newbie 4 02-17-2009 07:48 AM
Regular expressions using grep linuxmandrake Programming 3 11-16-2005 04:29 PM

All times are GMT -5. The time now is 02:51 AM.

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