質問

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.

役に立ちましたか?

解決

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.

他のヒント

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

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top