The UNIX and Linux Forums  

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
How to search for files based on the time stamp sunny_03 UNIX for Dummies Questions & Answers 1 02-12-2008 06:45 AM
Using find command for timestamp based search sunny_03 UNIX for Dummies Questions & Answers 1 02-12-2008 02:38 AM
column based search user_007 Shell Programming and Scripting 8 07-01-2007 02:52 AM
Using egrep to search for Text and special char izy100 UNIX for Advanced & Expert Users 2 11-04-2005 06:55 PM
search for hardlinks based on filename via find command hunternjb UNIX for Dummies Questions & Answers 2 03-26-2001 09:07 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 12-03-2004
Registered User
 

Join Date: Aug 2004
Posts: 141
Question Search based on 1st char of a row.

I want to search for the very first character in a row from the file.

It is very similar to the way we use Mainframes File-Aid, quick search option.

I couldnt find anything helpful with the grep command.

Does any one has an idea, how to perform this?
Reply With Quote
Forum Sponsor
  #2  
Old 12-03-2004
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
For example, say you wanted to print all lines from a file that started with
a lower-case "g", then do
grep '^g' file

Is this what you want?

Cheers
ZB
Reply With Quote
  #3  
Old 12-03-2004
Registered User
 

Join Date: Aug 2004
Posts: 141
Red face

Zazzybob

Though this solves my purpoase,
In future I might need to carry a search on criterias say
on 10th character or on 22nd character & 3 chars wide.

I still dont find logic to associate help on grep, to
solve the above problems also.
Reply With Quote
  #4  
Old 12-03-2004
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,653
To search for a g in column 4 you can do:

grep '^...g' filename


The ^ anchors the search to the left hand side of the line. Those dots match anything. Another way to do exactly the same thing is:

grep '^.\{3\}g'

You wouldn't do that here with just three dots, but at some point it start to make sense. To search for something 3 columns wide, specify 3 columns worth of stuff:

grep '^...cat'

will search for cat in columns 4 though 6. grep can do very much more...this just scratches the surface.
Reply With Quote
  #5  
Old 12-03-2004
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Indeed, regular expressions can be your best friend or worst enemy. Take the time to learn them as they are *very* powerful. Here are some links from the front page of a google search - (NOTE: I haven't checked - and therefore do not endorse - these links )

http://www.robelle.com/library/smugbook/regexpr.html
http://sitescooper.org/tao_regexps.html
http://www.grymoire.com/Unix/Regular.html

Beware that some regex constructs are specific to perl/sed/grep etc...

Cheers
ZB
Reply With Quote
  #6  
Old 12-06-2004
Registered User
 

Join Date: Aug 2004
Posts: 141
Thumbs up Got solution

Hi All,

With the links provided by Zazzybob, I could find a solution.


cat filename | cut -c m-n | grep '^String'

Let me explain -

cat command opens file with the name provided in loc of filename.

cut command cuts the characters from the range provided from m to n. Please note the very first char has an index as 1.

On these cut contents carry out search operation, from beginning as mentioned by ^, with the string in interest mentioned as 'String'.

And all you get is an expected result at console.

If anyone has a better approach than this, please share.
Reply With Quote
  #7  
Old 12-06-2004
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Just a note - you don't need the "cat". Just do

cut -c m-n file1 | grep '^String'

Cheers
ZB
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
perl, perl regex, regex, regular expressions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 03:01 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0