grep a pattern with line numbers.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep a pattern with line numbers.
# 1  
Old 02-22-2011
grep a pattern with line numbers.

I have a txt file with more than 10000 lines. There is a unique pattern which is scattered into the file. it starts with @9 and it has 15 characters. i need to grep them and display along with line numbers.

Eg: File - Test1
Code:
test message....
....
..
..
@9qwerty89
...test message @9qwerty56......
....end


Output should be as:
Code:
5: @9qwerty89
6: @9qwerty56


Can somebody help me in getting this.

Last edited by Franklin52; 02-23-2011 at 03:24 AM.. Reason: Please use code tags, thank you
# 2  
Old 02-22-2011
Is this fine..? Else post the inputfile with some more contents..
Code:
grep -in '^@9' inputfile > outfile

# 3  
Old 02-22-2011
How about this

Code:
$ cat Test1
test message....
.... @9qwertyNO skip this
..
..
@9qwerty8992081 more
...test message @9qwerty56l2dkv rest message @9Perty056l2dkv rest
....end
 
$ grep -Enow '\@9[[:alnum:]]{13}' Test1
5:@9qwerty8992081
6:@9qwerty56l2dkv
6:@9Perty056l2dkv


Last edited by Chubler_XL; 02-22-2011 at 01:47 AM..
# 4  
Old 02-22-2011
Quote:
Originally Posted by Chubler_XL
Code:
$ grep -Enow '\@9[[:alnum:]]{13}' Test1

"@" is an ordinary character in both posix regular expression flavors, basic and extended. According to the standard, in extended regular expressions, preceding an ordinary character with a backslash always yields an undefined result (in basic regular expressions, there are a few defined backslash-ordinary character sequences but none of them involved "@"). It's probably best to ditch "\@" in favor of an unescaped "@".

More info @ http://pubs.opengroup.org/onlinepubs...V1_chap09.html

Regards,
Alister

Last edited by alister; 02-22-2011 at 02:14 AM..
# 5  
Old 02-22-2011
Sample text for better understanding.

Sample file:
Code:
3628234::10120234236:042302134 SVP[3301]:: GE SupplychainInfomationBox::MART_LOOKING::value=@9querty34,day=Tues;
3628244::12342344444:042342344 SVP[3302]:: GE SupplychainInfomationBox::MART_LOOKING::value=@9querfy34,day=tend;
3345803::24555101206::243042304 SvP[3306]: SF SysPositionCar:DInfom::set kes_@9quefwe56-type_METHOD=null
2253245::10122432306::043442253 PLX[3301]: DC EManagersImplg:DsInform::soSuper-ult: search://mnt/var/docs/The%20set%20and%20unset-type_@9querwy91-shel_meth-w_1.wer
23434284::10120232336::233042253 EDK[3401]: SDRT Run:DISC NAME:put type=@9querwy91,

Here "@9" and followed by 8 digits are to be grepped and displayed along with the line numbers.

Eg output:
Code:
1:@9querty34
2:@9querfy34
3:@9quefwe56
4:@9querwy91
5:@9querwy91

Please let me know if i am not clear.

Last edited by Franklin52; 02-23-2011 at 03:24 AM.. Reason: Please use code tags
# 6  
Old 02-22-2011
Code:
sed -r 's/(.*)(@9.*[0-9][0-9])([^0-9].*)/\2/' file
@9querty34
@9querfy34
@9quefwe56
@9querwy91
@9querwy91

# 7  
Old 02-22-2011
Just drop the word match in grep:
Code:
grep -Eno '@9[[:alnum:]]{8}' infile

or if your grep does not know "-o" try this:
Code:
awk -F'[^@[:alnum:]]*' '{for(i=1;i<=NF;i++)if($i~/^@9........$/)print NR":",$i}' infile


Last edited by Scrutinizer; 02-22-2011 at 05:00 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Grep for a line containing only 5 numbers

How would you grep for a line containing only 5 numbers? Something like this. 10 2 12 1 13 (4 Replies)
Discussion started by: cokedude
4 Replies

2. Shell Programming and Scripting

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

3. Shell Programming and Scripting

How to Grep than scan line below grep pattern

Hello Colleagues, I have a file that looks like below. 6-12731913-12731913 9230760143480 410018547148230 20131002193434+0500 20131002193434+0500 ;20131002T161031000-10.50.241.21-21912131-1419034760, ver: 0 20131009 92220056296730 CC0P abc Core_Context_R1A SMS 6-12726796-12726796... (14 Replies)
Discussion started by: umarsatti
14 Replies

4. Shell Programming and Scripting

sed pattern fails to delete line of numbers

We are using Red Hat Linux. I have a flat file with among other things, the following lines, which appear occasionally throughout the file: Using sed, I delete this line: L;L;L;L;R;R;R;L;R;L;R;R;R;L;L;L With: /^;;;;;*/d Works fine every time. However, I cannot delete... (6 Replies)
Discussion started by: bloomlock
6 Replies

5. Shell Programming and Scripting

Select only line numbers using grep

Hai, I want to select only line numbers into a file if some pattern matches. I have written my script like below but its not working. #!/bin/sh file='/home/testfile1' filesearch='/home/test00' while read line do search=`echo $line |cut -c 1-24` echo $search echo `grep -n ""... (3 Replies)
Discussion started by: Subbu123
3 Replies

6. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

7. UNIX for Dummies Questions & Answers

Grep lines with numbers greater than 2 digits at the end of the line

I'm trying to grep lines where the digits at the end of each line are greater than digits. Tried this but it will only allow me to specify 2 digits. Any ideas would greatly be appreciated. grep -i '\<\{3,4,5\}\>' file ---------- Post updated at 05:58 PM ---------- Previous update was at 05:41... (1 Reply)
Discussion started by: jimmyf
1 Replies

8. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

9. UNIX for Dummies Questions & Answers

Get line numbers while searching the pattern in log

Hi Folks, I am searching for a pattern in logs through putty by opening the file in vi editor and reaching to the last of the file by $ and then searching the pattern , lets say I have to search the pattern abc then it would be ?abc Now I want line numbers along with the matching pattern to be... (3 Replies)
Discussion started by: SankalpS
3 Replies

10. UNIX for Dummies Questions & Answers

grep to get line numbers

I know if i use grep -n that the output will have the lines numbered but is there a way to grep the actually line number. so like this grep -n "one" /usr/dict/numbers 1:one 21:twenty-one 31:thirty-one 41:forty-one 51:fifty-one 61:sixty-one 71:seventy-one 81:eighty-one 91:ninety-one ... (1 Reply)
Discussion started by: alindner
1 Replies
Login or Register to Ask a Question