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

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 12-03-2004
videsh77 videsh77 is offline
Registered User
  
 

Join Date: Aug 2004
Posts: 144
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?
  #2 (permalink)  
Old 12-03-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
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
  #3 (permalink)  
Old 12-03-2004
videsh77 videsh77 is offline
Registered User
  
 

Join Date: Aug 2004
Posts: 144
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.
  #4 (permalink)  
Old 12-03-2004
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
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.
  #5 (permalink)  
Old 12-03-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
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
  #6 (permalink)  
Old 12-06-2004
videsh77 videsh77 is offline
Registered User
  
 

Join Date: Aug 2004
Posts: 144
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.
  #7 (permalink)  
Old 12-06-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
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
Sponsored Links
Closed Thread

Bookmarks

Tags
perl, perl regex, 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 04: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