Search based on 1st char of a row.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Search based on 1st char of a row.
# 1  
Old 12-03-2004
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  
Old 12-03-2004
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  
Old 12-03-2004
Power

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  
Old 12-03-2004
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  
Old 12-03-2004
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 Smilie )

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  
Old 12-06-2004
MySQL 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  
Old 12-06-2004
Just a note - you don't need the "cat". Just do

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

Cheers
ZB
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting single row into multiple rows based on for every 10 digits of last field of the row

Hi ALL, We have requirement in a file, i have multiple rows. Example below: Input file rows 01,1,102319,0,0,70,26,U,1,331,000000113200000011920000001212 01,1,102319,0,1,80,20,U,1,241,00000059420000006021 I need my output file should be as mentioned below. Last field should split for... (4 Replies)
Discussion started by: kotra
4 Replies

2. Shell Programming and Scripting

Search a certain char and insert new text if a match found

Have a file which has the create statement like below create table emp ( empno integer, empname char(50)) primary index(empno); i need to find a string starting with create and ends with semi-colon ;. if so insert the below statement before create statement rename table emp to emp_rename;... (2 Replies)
Discussion started by: Mohan0509
2 Replies

3. Shell Programming and Scripting

How to mark the row based on col value.?

Hi Gurus, I have requirement to identify the records based on one column value. the sample file as below: ID AMT, AMT1 100,10, 2 100,20, 3 200,30, 0 200, 40, 0 300, 20, 2 300, 50, 2 400, 20, 1 400, 60, 0 for each ID, there 2 records, if any one record amt1 is 0, the in 4th col add... (5 Replies)
Discussion started by: ken6503
5 Replies

4. Shell Programming and Scripting

Search row by row from one file to another file if match is found print few colums of file 2

this is the requirement list.txt table1 table2 table3 testfile.txt name#place#data#select * from table1 name2#place2#data2#select * from table 10 innerjoin table3 name2#place2#data2#select * from table 10 output name place table1 name2 place table3 i tried using awk (7 Replies)
Discussion started by: vamsekumar
7 Replies

5. Shell Programming and Scripting

To search distinct special char in file

hi, i need to search distinct special character from a file which is a pipe delimited from a specific column for eg: input file.txt aa|bb|cc|$abc aa|bb|ccc|#abol bb|xss|ddd|$xyz nn|yyy|qqq|=qqqq abe|qqq|yyy|=aaa aaa|yyy|zzzz|#aaaa . . . my desired output $ (10 Replies)
Discussion started by: rohit_shinez
10 Replies

6. Shell Programming and Scripting

Search a row

Hi, I would like to capture rows having the following format : BIND dn="uid=euser00,ou=people,o=example.com" except "euser00" which is user id, others should remain as it is. Here is what I can try so far : BIND dn="\s*?uid=s* Also, people or example can be of any case (... (4 Replies)
Discussion started by: susankoperna1
4 Replies

7. Shell Programming and Scripting

To search null and char in file

Hi guys, I want to know how i can search null value and character value in a feed file. my file has Pipe delimted records. for eg: 1|12|xxx|yyy . . . N records I know the the which column null and char occurs does it has for eg 3rd column which i can obtain by awk $3 and (7 Replies)
Discussion started by: rohit_shinez
7 Replies

8. Shell Programming and Scripting

Splitting a variable based on newline char

Heeloo all, A weird problem perhaps. May god save others from this problem. I want to print each line from a variable.. the example below should make it clear. smvar="Hello World1 Hello world 2 forgot there I guess" for eachline in $smvar echo $eachline end Whats for... (3 Replies)
Discussion started by: pavanlimo
3 Replies

9. Shell Programming and Scripting

grep : search a long char contain space

Hi, i have to search for a char like that : export var1="i am not happy /not happy" with a command like : grep $var1 file but this not working with me !!! thank you in advance. (2 Replies)
Discussion started by: tizilfin
2 Replies

10. UNIX for Advanced & Expert Users

Using egrep to search for Text and special char

Anyone is well-versed to use egrep to search a file for a line containing both: 1) AAA 2) $ I am having problem escaping the dollar sign when using egrep in conjunction with satisfying AAA as well. E.g. Text file Line 1 AAA Line 2 $$$ Line 3 AAA BBB $ Line 4 $$$ BBB AA will return me... (2 Replies)
Discussion started by: izy100
2 Replies
Login or Register to Ask a Question