views:

52

answers:

6

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)

+1  A: 

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.

unwind
A: 

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

Syntax => grep pattern filename
examlpe
grep amet /path_to_file
Ibrahim
+3  A: 
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 ($).

Bart Sas
+1  A: 

use awk

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

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.

Dennis Williamson
A: 
ruby -ne 'print unless 1 .. /baz[0-9]/' file