![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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? |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
Just a note - you don't need the "cat". Just do
cut -c m-n file1 | grep '^String' Cheers ZB |
||||
| Google The UNIX and Linux Forums |
| Tags |
| perl, perl regex, regex, regular expressions |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|