How to grep at nth position?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to grep at nth position?
# 1  
Old 04-25-2008
How to grep at nth position?

Suppose the data is as follows:

123456789A123456
123456789A123456
123456789C123456
123456789B123456
123456789A123456

I want to grep only those records containing "A" command can be:
grep "^.........A" file_name

But if data string is very long, suppose A occurs at 690th position.
Its doesn't look good giving 690 dots and then "A"....I am sure there must be some alternative.

Please help!

-Sumit
# 2  
Old 04-25-2008
Code:
egrep '^.{690}A' file_name

# 3  
Old 04-25-2008
It works.

Thanks a lot!
sumit
# 4  
Old 04-25-2008
Sorry, if column number is less than 255 then it works if it is more than 255 its giving error
--
$egrep '^.{256}A' fl
egrep: context of \{\} invalid
# 5  
Old 04-25-2008
Any further help?
# 6  
Old 04-25-2008
Find a less broken version of egrep (installing the GNU tools will pay itself back a million times), or use a different tool which supports the same syntax.

Code:
perl -ne 'print if m/^.{690}A/' file

# 7  
Old 04-25-2008
Era you are a genius....perl works perfectly!!

Thanks once again!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep a pattern in a key position a file

Hi guys, So I have a file containing data of a marathon. Here's an example what it looks like including the given key: # key: sex, time, athlete, athlete's nationality, date, city, country M, 2:30:57.6, Harry Payne, GBR, 1929-07-05, Stamford Bridge, England M, 2:5:42, Khalid Khannouchi,... (3 Replies)
Discussion started by: DNM_UKN
3 Replies

2. Shell Programming and Scripting

To find nth position of character in string

Hi guyz i want to know nth position of character in string. For ex. var="UK,TK,HK,IND,AUS" now if we see 1st occurance of , is at 3 position, 2nd at 6,..4th at 13 position. 1st position we can find through INDEX, but what about 2nd,3rd and 4th or may be upto nth position. ? In oracle we had... (2 Replies)
Discussion started by: Jonty Immortal
2 Replies

3. Shell Programming and Scripting

How to grep nth word in line?

my input file content is like this GEFITINIB 403 14 -4.786873 -4.786873 -1.990111 0.000000 0.000000 -1.146266 -39.955912 483 VANDETANIB 404 21 -4.754243 -4.754243 -2.554131 -0.090303 0.000000 -0.244210 -41.615502 193 VANDETANIB 405 21 -4.737541 -4.737541 -2.670195 -0.006006 0.000000 -0.285579... (4 Replies)
Discussion started by: chandu87
4 Replies

4. Shell Programming and Scripting

Grep for a string at a certain position

trying to grep for a string at a certain position in a commands output in order to use it as a variable: route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default 192.168.2.1 0.0.0.0 UG 0 0 0 wlan1... (4 Replies)
Discussion started by: 3therk1ll
4 Replies

5. Emergency UNIX and Linux Support

Replace nth position character of all the lines in file

I want to replace 150th character of all the lines in a file using sed or awk... searched the forums but didn't find exact answer (9 Replies)
Discussion started by: greenworld123
9 Replies

6. Shell Programming and Scripting

Grep starting from a specific position

Hello people, I'm scratch my head to find a solution to my problem, I'm absolutely sure this is very simple!!! :wall: I'm using the tcpdump to show on the screen in real time the UCP traffic: tcpdump -l -i bond1 -s 1514 -nntttt -A src or dst 192.168.1.5 and port 10000 | egrep "/51/"The output... (5 Replies)
Discussion started by: Lord Spectre
5 Replies

7. UNIX for Dummies Questions & Answers

Using grep to check for character at fixed position

i have a file (test.txt) that contains: 20799510617900000928000000005403020110315V 20799510617900000928000000005403020110316 20799510617900000928000000005403020110317 20799510617900000928000000005403020110318V grep V test.txt > /tmp/void.log if then mail -s "void" < test.txt fi... (2 Replies)
Discussion started by: tjmannonline
2 Replies

8. Shell Programming and Scripting

Print lines with specific character at nth position in a file

I need to print lines with character S at nth position in a file...can someone pl help me with appropriate awk command for this (1 Reply)
Discussion started by: manaswinig
1 Replies

9. Shell Programming and Scripting

Print lines with specific character at nth position in a file

I need to print lines with character S at nth position in a file...can someone pl help me with appropriate awk command for this (2 Replies)
Discussion started by: manaswinig
2 Replies

10. UNIX for Dummies Questions & Answers

grep position 44-46

i have a content that looks like this: 0100THE CHILDREN'S HOSP OF PHILA 000123614A0057701200000 i used: grep A00, and it worked fine, until i have a few other A00 sit at position 66 and 105 and so on. i want to grep A00 only at position 44-46. (2 Replies)
Discussion started by: tjmannonline
2 Replies
Login or Register to Ask a Question