![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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... ![]() |
|
||||
|
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 |
|
||||
|
Quote:
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.. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|