Kit Ho
Kit Ho

Reputation: 26958

How to grep this kind of pattern out using this "grep" command?

In file named appleFile:

1.apple_with_seeds###
2.apple_with_seeds###
3.apple_with_seeds_and_skins###
4.apple_with_seeds_and_skins###
5.apple_with_seeds_and_skins###
.....
.....
.....

How can i use the grep command to grep the pattern only with "apple_with_seeds"??? It is supposed that there is random characters after seeds and skins.

Result:
1.apple_with_seeds###
2.apple_with_seeds###

Upvotes: 1

Views: 543

Answers (3)

bmk
bmk

Reputation: 14137

Maybe something like this will work for you:

grep 'apple_with_seeds[^_]' appleFile

That will print all lines having no _ character after seeds. You can add other characters to exclude to between the brackets (but after the ^), e.g. [^_a-z] will additionally exclude all lower case letters.

Or you could explicitly include some characters (like #):

grep 'apple_with_seeds[#]*$' appleFile

And again you can add arbitrary characters between the brackets, e.g. [#A-Z] would match any of the characters # or A-Z.

Upvotes: 2

James.Xu
James.Xu

Reputation: 8295

cat appleFile | grep "apple_with_seeds$"

UPDATE: if you want to exclude something, try -v option:

 cat appleFile | grep "apple_with_seeds$" | grep -v "exclude_pattern"

Upvotes: 2

Rahul
Rahul

Reputation: 77846

Try this

cat appleFile|grep -i seeds$

Upvotes: 0

Related Questions