LinuxQuestions.org
Review your favorite Linux distribution.
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 02-10-2009, 11:35 AM   #1
iongrey
LQ Newbie
 
Registered: Feb 2009
Posts: 7

Rep: Reputation: 0
Searching a line


Greetings everyone,

I am doing some beginners practicing with searching files in Unix and am having trouble understanding how to search a line that starts with some alphanumeric character and ends with some alphanumeric character. For example the following lines:

Test Test1 Test2 Test3
Be3st Ce4st1 Fest2 Mest6

If I wanted to find the line that starts with T and ends with 3, how would I do that?

I tried egrep '^T ? 3$', grep '^[T-3$]' and other variants, but no luck.

Any help would be appreciated.

Thanks and please be kind.
 
Old 02-10-2009, 12:28 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
In regex, '.' (period) represents any single character. '*' means "match the preceding character (or character range) zero or more times", '?' means "match the preceding character zero or one times", and '+' means "match the preceding character one or more times. So what you want is probably something more like '^T.*3$' or '^T.+3$', depending on whether you expect anything to exist between the first and last characters (the second one would match "Test3", but not "T3").

You can specify a range of characters or numbers with brackets, so '^[a-zA-Z].+[0-9]$' will match any letter of the alphabet at the beginning of the line, followed by one or more characters of any type, and ending with any number.

There are some special character range types also, like [:alnum:], which represents any alphanumeric character. You can find these documented in the grep man page.

There are many good regular expression tutorials on the web. I very highly recommend taking the time to work through one. You'll be glad you did.

Last edited by David the H.; 02-10-2009 at 12:34 PM. Reason: added recommendation at the end
 
Old 02-10-2009, 12:48 PM   #3
iongrey
LQ Newbie
 
Registered: Feb 2009
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks David The H. for clarification! It really helped.
 
Old 02-10-2009, 01:04 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
No problem. By the way, you can use the -o option in grep/egrep to output only the matching strings, instead of the full line. Makes it easier to see what's actually going on.
 
  


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
Searching for regular expression in command line Jean Of mArc Programming 9 08-11-2008 08:01 PM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 PM
Searching for FTP SSL command line client for Fedora chipix Linux - Security 4 06-27-2006 08:47 AM


All times are GMT -5. The time now is 07:47 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