linux - How to grep both header and pattern with grep -
i need grep both header , particular pattern using grep,
eg
for command "ps" output
pid tty time cmd 10280 pts/16 00:00:00 ps 32463 pts/16 00:00:00 bash
how can grep both header , pattern 32463 output should
pid tty time cmd 32463 pts/16 00:00:00 bash
and 1 thing solution should general means should applicable commands have headers
how can grep both header , pattern
you try this
ps | grep -e 'pid\|32463'
solution should general means should applicable commands have headers
this requirement impossible satisfy
grep
, because different commands have different headers, impossible assign regular expression match of them.but use following command achieve goal:
command | perl -e 'while(<stdin>) { print if $. == 1 or m/$argv[0]/ }' pattern
if cumbersome daily use, can put in custom script, such
my-grep
, , put script in$path
, can use script normal command:command | my-grep pattern
Comments
Post a Comment