Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

filter each line of linux bash output by regexp [closed]

Tags:

I want to filter the output of arbitrary output e.g. cat or objdump to only display lines which contain "pattern".

Is there a one-liner UNIX/Linux command to do this?

e.g. cat filepath | xargs grep 'pattern' -l is not working for me

like image 393
T. Webster Avatar asked Mar 22 '13 20:03

T. Webster


People also ask

How do I filter a line in Linux?

In Linux the grep command is used as a searching and pattern matching tools. The most common use of grep is to filter lines of text containing (or not containing) a certain string. You can write this without the cat. One of the most useful options of grep is grep -i which filters in a case insensitive way.

What two Linux commands can be used for pattern matching and filtering of output?

Grep, Egrep, Fgrep, Rgrep Commands These filters output lines matching a given pattern.

How to use grep to filter output?

grep is very often used as a "filter" with other commands. It allows you to filter out useless information from the output of commands. To use grep as a filter, you must pipe the output of the command through grep . The symbol for pipe is " | ".


2 Answers

cat file | grep pattern 

You could also just use grep pattern file if it's a static file.

like image 65
Explosion Pills Avatar answered Sep 25 '22 07:09

Explosion Pills


Better to use grep -e or egrep(this allows for extended regular expressions). Then you can do more robust things with regex:

 cat my_phonebook | egrep "[0-9]{10}" 

To show all 10 digit phone numbers in a file.

If you toss in a -o, only the numbers get returned (instead of the before and after content on the line).

like image 29
Paul Calabro Avatar answered Sep 23 '22 07:09

Paul Calabro