开发者

how to display all lines from one that match regex in linux

I want to display all lines from one which match regular expression

if I have a file开发者_运维知识库

foo
bar123
baz12435
lorem
ipsum
dolor
sit
amet

this display-from baz[0-9]* < file sould return (It doesn't matter if it display matched line or not)

lorem
ipsum
dolor
sit
amet

How can I do this in Linux (with sed, awk or grep)


Just use grep:

$ grep -E 'baz[0-9]*' file -A9999

Here, the '-A' option tells grep how many lines to display after the match. It's a bit clunky, but if there's an upper bound on the length of your input files, it might work OK.


sed -e '/baz[0-9]*/,$b' -e 'd' file

This will delete all lines from the input except the ones from the first line that matches the regex (/baz[0-9]*/) to the end of the file ($).


The accepted answer prints the line which includes the pattern. If you want to exclude that line:

sed  '1,/baz[0-9]*/d' file

A simpler version of the accepted answer:

sed  -ne '/baz[0-9]*/,$p' file

Some versions of sed don't need the -e except in certain circumstances (but accept it anyway) and some do. These examples illustrate both types.


use awk

awk '/baz/{f=1;next}f' file


I use grep to search a keyword in a file. I use the following:

Syntax => grep pattern filename
examlpe
grep amet /path_to_file


ruby -ne 'print unless 1 .. /baz[0-9]/' file
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜