grep the number of first occurence


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grep the number of first occurence
# 1  
Old 07-11-2008
grep the number of first occurence

File1.txt

.......
.......
OMC LA
OMC LK
OMC LS
........
........

Above is the content of File1.txt, i want to get the Number of Occurence to order, lets say if OMC LA = 1, OMC LS=3, and OMC LK=2..

omc_ident="OMC LA" or "OMC LK" or "OMC LS"

omc_num=`grep '^OMC' File1.txt| grep -n '^OMC $omc_ident$' | awk -F: '{ print $1 }'`

IF omc_ident is "OMC LS", then the omc_num is 3....

but in return i get a null value, any1 can help me to find out how i can correct this script...
Smilie
# 2  
Old 07-11-2008
Lots of things here:
the second 'grep' is looking for "OMC OMC LS" - you've got OMC followed by $omc_ident, which is set to "OMC LA" or something similar. Unfortunately, you've enclosed this in single quotes so what it is actually looking for is "OMC $omc_ident$" - variables aren't expanded inside single quotes. Also, grep -n prints the line number and line for each match, not the number of occurrences, use "grep -c" for a count. Then, why awk?

Try:
omc_num=`grep -c "^${omc_ident}$" File1.txt`

For one-liners like this it's easier to build them up gradually then multiple errors don't disguise each other -then look at simplifying.
if you'd tried
grep '^OMC' File1.txt and looked at the output
then
grep '^OMC' File1.txt| grep -n '^OMC $omc_ident$' and seen no output
then you would have improved your chances of solving the problem.
And read the man pages!
cheers
Steve
# 3  
Old 07-11-2008
Quote:
Originally Posted by thestevew
Lots of things here:
the second 'grep' is looking for "OMC OMC LS" - you've got OMC followed by $omc_ident, which is set to "OMC LA" or something similar. Unfortunately, you've enclosed this in single quotes so what it is actually looking for is "OMC $omc_ident$" - variables aren't expanded inside single quotes. Also, grep -n prints the line number and line for each match, not the number of occurrences, use "grep -c" for a count. Then, why awk?

Try:
omc_num=`grep -c "^${omc_ident}$" File1.txt`

For one-liners like this it's easier to build them up gradually then multiple errors don't disguise each other -then look at simplifying.
if you'd tried
grep '^OMC' File1.txt and looked at the output
then
grep '^OMC' File1.txt| grep -n '^OMC $omc_ident$' and seen no output
then you would have improved your chances of solving the problem.
And read the man pages!
cheers
Steve

Thanks for explanation but yet it didnt solve my problem steve.....
grep '^OMC' File1.txt| grep -n '^OMC $omc_ident$' is return null as ur comment..
Pls let me know if there is any possible solution for this....

Last edited by neruppu; 07-11-2008 at 06:00 AM..
# 4  
Old 07-11-2008
Is this what you're after :

Code:
#  cat infile
OMC LA
OMC LK
OMC LS
OMC LA
OMC LK
OMC LA


#  sort infile | uniq -c | sort -n
   1 OMC LS
   2 OMC LK
   3 OMC LA

HTH,
# 5  
Old 07-11-2008
The problem is solved...


omc_num=`grep '^OMC' File1.txt| grep -n "^OMC $omc_ident$" | awk -F: '{ print $1 }'`
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print occurence number

Hi folks, I have a file with lots of lines in a text file,i need to print the occurence number after sorting based on the first column as shown below, thanks in advance. sam,dallas,20174 sam,houston,20175 sam,atlanta,20176 jack,raleigh,457865 jack,dc,7845 john,sacramento,4567 ... (4 Replies)
Discussion started by: tech_frk
4 Replies

2. Programming

Python Count Number Of Occurence

Hello, I have a programming assignment to count number of occurrences of hours in particular file. Below is the code: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" largest = None fh = open(fname) counts = dict() test = list() for line in fh: ... (2 Replies)
Discussion started by: infinitydon
2 Replies

3. UNIX for Dummies Questions & Answers

Add number of occurence

Good morning, I need help to add number of occurence based on column 1 & column 5 file input 81161267334|1|100000|81329998077|20150902 81161267334|1|50000|82236060161|20150902 81161268637|1|25000|81329012229|20150911 81161269307|1|25000|81327019134|20150901... (3 Replies)
Discussion started by: radius
3 Replies

4. Shell Programming and Scripting

Split column into two on first occurence of any number

Input : abc def 1 xyz zzz bca cde 2 yyy xxx Expected output : abc def |1 xyz zzz bca cde |2 yyy xxx I have tried the command below and losing the number. Any help is greatly appreciated 1. sed 's//|/' num.txt Result: abc def | xyz zzz bca cde |... (7 Replies)
Discussion started by: kbsuryadev
7 Replies

5. Shell Programming and Scripting

Number each occurence using sed

hi, I need to number each occurrence of a pattern within a file using sed. Given object 0000 object 111 object 222 I need following 1.object 0000 2.object 111 3.object 222 (5 Replies)
Discussion started by: xerox
5 Replies

6. Shell Programming and Scripting

finding the number of occurence of a word in a line

suppose i have this line abs|der|gt|dftnrk|dtre i want to count the number of "|" in this line.. how can i do that. plz help:confused: (9 Replies)
Discussion started by: priyanka3006
9 Replies

7. UNIX for Dummies Questions & Answers

number of occurence using grep -c then assigning it to a variable

Hi guys! I need to count the occurence of a certain pattern. For example the pattern is PC. the contents of the file sample.txt: A PC asdfgadfjkl asdfa PC sadfaf fdsPCasdfg if i use grep -c PC sample.txt it will display 3 as the number of occurence how do i save that number to a... (1 Reply)
Discussion started by: khestoi
1 Replies

8. Shell Programming and Scripting

first occurence and line number

Hi, My objective is to get the line number of the first occurance of the search pattern. my test.txt contains: ..... .................. total rows.... ................... .. total rejected rows: 40 total rejected rows: 50 total rejected rows: 80 total rejected rows: 90 total... (9 Replies)
Discussion started by: mercuryshipzz
9 Replies

9. UNIX for Dummies Questions & Answers

First occurence from grep

Hi , supoose i have a file in which a word is repeated so many times. I just want the firts occurence of that word through grep and it should not go to the next one means get the first occurence and stop there. Suggest me some solutions. Thanks Namish (10 Replies)
Discussion started by: namishtiwari
10 Replies

10. UNIX for Dummies Questions & Answers

grep for certain occurence

I am using grep to pull out info from a file. The line I am searching for begins: START TIME - Tue Sep 11 16:40:00. There are mutiple lines of START TIME. I need the FIRST occurence ONLY. My grep is as follows: start="$( grep 'START TIME' filename | cut -c15-33)" If I echo or cat... (2 Replies)
Discussion started by: app4dxh
2 Replies
Login or Register to Ask a Question