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
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.
Grep, Egrep, Fgrep, Rgrep Commands These filters output lines matching a given pattern.
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 " | ".
cat file | grep pattern
You could also just use grep pattern file
if it's a static file.
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).
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