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)
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.
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.
Using the grep Command. If we use the option '-A1', grep will output the matched line and the line after it.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With