Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 603
jcubic Avatar asked Sep 15 '10 14:09

jcubic


People also ask

How do you grep all lines?

To include all subdirectories in a search, add the -r operator to the grep command. This command prints the matches for all files in the current directory, subdirectories, and the exact path with the filename. In the example below, we also added the -w operator to show whole words, but the output form is the same.

How do I grep a whole line in Linux?

4. Checking for the whole words in a file : By default, grep matches the given string/pattern even if it is found as a substring in a file. The -w option to grep makes it match only the whole words.

How do you grep a line after a match?

Using the grep Command. If we use the option '-A1', grep will output the matched line and the line after it.

How do you use grep to find lines ending with the pattern?

You can specify a pattern to search with either the –e or –f option. If you do not specify either option, grep (or egrep or fgrep) takes the first non-option argument as the pattern for which to search. If grep finds a line that matches a pattern, it displays the entire line.


1 Answers

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.

like image 96
unwind Avatar answered Nov 19 '22 23:11

unwind