Daniel Del Core
Daniel Del Core

Reputation: 3221

Bash grep command starts with or ends with

I'm after a command that will return results based on a pattern match that starts with or ends with a the given pattern.

This is what i have so far.

"cat input.txt | grep "^in|in$"

My main problem is that i cant get the (or) to work but i can get them to work individually.

Thanks for your help in advance.

Upvotes: 4

Views: 25783

Answers (3)

Galzzly
Galzzly

Reputation: 13

Have you thought of using egrep rather than grep? Using the following should work for what you're after:

egrep "^in|in$" input.txt

There's no need to have the cat at the start, the above will work fine.

Upvotes: 0

Kent
Kent

Reputation: 195169

try this:

grep "^in\|in$" input.txt

by default, grep use BRE, you have to escape the |. Or use grep's -E or -P, in order to avoid escaping those char with special meaning.

P.S, the cat is no necessary.

Upvotes: 12

Jotne
Jotne

Reputation: 41460

This awk should work:

awk '/^start|end$/' file

It will print all lines starting with start or ending with end

cat file
nothing
start with this
or it does have an end
or the end is near

awk '/^start|end$/' file
start with this
or it does have an end

Upvotes: 0

Related Questions