문제

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