The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



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 !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
first occurence and line number mercuryshipzz Shell Programming and Scripting 9 01-22-2008 07:42 PM
First occurence from grep namishtiwari UNIX for Dummies Questions & Answers 10 08-23-2007 08:44 AM
Count the number of occurence of perticular word from file rinku Shell Programming and Scripting 40 08-10-2007 07:33 PM
grep the last occurence apalex UNIX for Dummies Questions & Answers 3 04-18-2002 06:03 PM
grep for certain occurence app4dxh UNIX for Dummies Questions & Answers 2 09-14-2001 03:40 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-11-2008
neruppu neruppu is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 13
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...
  #2 (permalink)  
Old 07-11-2008
thestevew thestevew is offline
Registered User
  
 

Join Date: Mar 2006
Location: South Yorkshire, UK
Posts: 114
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 (permalink)  
Old 07-11-2008
neruppu neruppu is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 13
Quote:
Originally Posted by thestevew View Post
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 05:00 AM..
  #4 (permalink)  
Old 07-11-2008
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
  
 

Join Date: Jun 2003
Location: Scotland
Posts: 431
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 (permalink)  
Old 07-11-2008
neruppu neruppu is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 13
The problem is solved...


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

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:04 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0