grep an exact number

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
trio
Posts: 2076
Joined: Sun 21 Dec 2008, 15:50
Location: अनà¥￾मोदना

grep an exact number

#1 Post by trio »

Dear all,

Please help this dummy, I want to grep an exact number, how to do that? And yes, I am stupid.

Example:
in a file you have lines numbered from 1 to 100, you want to grep exactly line number 9, but I do:

Code: Select all

grep 9 /path/to/file.txt
I will get lines numbered 9, 19, 29, etc But I want only line number 9

Thanks

User avatar
dejan555
Posts: 2798
Joined: Sun 30 Nov 2008, 11:57
Location: Montenegro
Contact:

#2 Post by dejan555 »

you can use sed:

Code: Select all

sed -n '9p' /path/to/file.txt
puppy.b0x.me stuff mirrored [url=https://drive.google.com/open?id=0B_Mb589v0iCXNnhSZWRwd3R2UWs]HERE[/url] or [url=http://archive.org/details/Puppy_Linux_puppy.b0x.me_mirror]HERE[/url]

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#3 Post by seaside »

Trio,

Also,

Code: Select all

grep -w 9 /path/to/file.txt
The "-w" matches a single word only

s

Shel
Posts: 103
Joined: Sat 11 Apr 2009, 17:33
Location: Seattle, WA, USA, or Southern France

#4 Post by Shel »

If the number "9" is at the beginning of a line, you can check for that:

Code: Select all

grep '^9' filename
... of if it's followed with a space, you can check for that:

Code: Select all

grep '9 ' filename
or combine the two, etc. grep will do fairly complex regular expressions, so you can look for a "9" at the beginning of a line, followed by, say, a capital letter:

Code: Select all

grep '^9[A-Z]' filename
I think those are all correct, I'm not in a position to check them at the moment, but doco for grep is all over the 'net.

-Shel

User avatar
trio
Posts: 2076
Joined: Sun 21 Dec 2008, 15:50
Location: अनà¥￾मोदना

#5 Post by trio »

Thx all...seaside's answer did the job

Post Reply