Link to home
Start Free TrialLog in
Avatar of Amulya
Amulya

asked on

grep/sed/awk

I want to look for a particular word in a file, say, eg.
When i use grep i get any match with eg, like eg1, eg2,eg3.
I want to find out the exact line number on which eg is occuring in the file.
For this i have used
 num= grep -n "eg" file | cut -d":"-f1
  where file is the file i am searching
this would asssign all the line numbers on which any eg(eg1, eg2...) is found to num. I do not want this , I want only the line on whihc eg is found
I guess ihave to use something else
I would appreciate any hints
Thank you in advance
Avatar of manavpreet_t
manavpreet_t

if the word which you are searching is at the end of line,
try
grep -n word$ filename
{ here you put a dollar sign }
else if it is in between
try grep -n "word " filename
{ here you put a space }
Avatar of Amulya

ASKER

eg will always occur at the beginning, so when i use grep, i always get the other matches as well.
Is there anyway to obtain an exact match?
even if it occurs at the beginning,
when you put
grep -n "eg " filename
you will get an exact match but be careful to put the single spave after eg
i mean single space after eg
grep -n "eg(space here)" filename
Avatar of Amulya

ASKER

Since there are no spaces after the line , it will not find any eg,i tried putting the space, earlier,
 and it did not work
The file looks like
eg
eg1
eg2
eg3
eg24
there are no spaces
ASKER CERTIFIED SOLUTION
Avatar of manavpreet_t
manavpreet_t

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ozo
grep -n '\<eg\>'
grep -n -w 'eg'
As ozo has stated above, but slightly different to stop results when eg occurs other than the beginning of the line;
grep -n '^eg\>' file
Hi, I would have used awk for this:
cat <filename> | awk 'BEGIN{count=0}{count++}{if($1=="eg")print count}'

This works only if the first word on the line matches "eg" exactly, and regardless of any other contents in the line.
If "eg" is ALWAYS the only word in the line, you might as well use $0 instead of $1, meaning "eg" mathces the whole line.

Thanks for returning here, and finalizing one open question.  BUT please update and finalize all the other open questions you have as well.  There are 13 of them.....
Some are current, many old.  If you need special handling help in any of them comment with details and I'll respond when I can.  The HELP DESK link on the left provides guidance in terms of Member Agreement, Guidelines and the Question/Answer processes.

If you award anything less than an "A" grade, please be sure to add comments as to why.

https://www.experts-exchange.com/jsp/qShow.jsp?ta=unix&qid=20108784
https://www.experts-exchange.com/jsp/qShow.jsp?ta=progsoftgen&qid=20107243
https://www.experts-exchange.com/jsp/qShow.jsp?ta=cprog&qid=20164605
https://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20195786
https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20263024
https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20262125
https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20261705
https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20259183
https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20259140
https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20243128
https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20201666
https://www.experts-exchange.com/jsp/qShow.jsp?ta=perl&qid=20182968
https://www.experts-exchange.com/jsp/qShow.jsp?ta=perl&qid=20154466

I will monitor them all and update each of them for you with this information so you can quickly and easily navigate through them, and to ensure that you receive Email notifications of these requests.

Expert input is always welcome if more is needed, and/or these remain open after one week so we can determine the fair outcome of the older items.

Thanks all,

Moondancer
Community Support Moderator @ Experts Exchange

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
- Answered by manavpreet_t

Please leave any comments here before 13/1/2003

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

tfewster (I don't work here, I'm just an Expert :-)
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange