grep保留标题的方法

  1. awk 方法
https://www.cnblogs.com/xudong-bupt/p/3721210.html

$ ps -ef |  awk 'NR==1{print $1,$2,$8} /java|mysql/{print $1, $2, $8}'
UID PID CMD
mysql 34948 /usr/sbin/mysqld
int4 298274 awk

# 显示2列以后的数据
./pidstat -p ALL -r | awk 'NR==3{ for(i=1; i<=2; i++){ $i="" }; print $0 }/ServiceStarter/{ for(i=1; i<=2; i++){ $i="" }; print $0 }'
  1. head 方法
# 将实际内存消耗最大的10个进程显示出来的命令:

$ ps auxw|head -1;ps auxw|sort -rn -k6|head -10
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root       851  0.2  1.6 408816 33224 ?        Ssl  01:21   0:11 /usr/bin/docker daemon -H fd://
root      8452  0.0  0.3  95584  7212 ?        Ss   01:47   0:00 sshd: tiger [priv]
root       980  0.0  0.3  95464  7088 ?        Ss   01:21   0:00 sshd: tiger [priv]
root      1044  0.0  0.3  95464  7048 ?        Ss   01:23   0:00 sshd: tiger [priv]
root       854  0.0  0.3  65612  6616 ?        Ss   01:21   0:00 /usr/sbin/sshd -D
root         1  0.0  0.2  37888  5952 ?        Ss   01:21   0:02 /sbin/init noprompt
syslog     576  0.0  0.2 256396  5372 ?        Ssl  01:21   0:00 /usr/sbin/rsyslogd -n
  1. sed 方法
# 打印第一行,删除不是syslog的行
$ ps aux | sed -e '1p' -e '/syslog/!d'    
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
message+     567  0.0  0.0  12928  9928 ?        Ss   5月07   1:22 /usr/bin/dbus-daemon
syslog       597  0.0  0.0 224332  4960 ?        Ssl  5月07   0:07 /usr/sbin/rsyslogd -n -iNONE
int4      298270  0.0  0.0  17800   784 pts/1    R+   14:30   0:00 sed -e 1p -e /syslog/!d

  1. grep 方法
# 1 带行号
$ ps aux | grep -n '.*' | grep -e '(^1:)|syslog'
1:USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
113:message+     567  0.0  0.0  12928  9928 ?        Ss   5月07   1:22 /usr/bin/dbus-daemon --system --address=systemd
117:syslog       597  0.0  0.0 224332  4960 ?        Ssl  5月07   0:07 /usr/sbin/rsyslogd -n -iNONE
254:int4      298286  0.0  0.0  17676  2700 pts/1    S+   14:36   0:00 grep --color=auto -e (^1:)|syslog

# 2 去掉行号
$ ps aux | grep -n '.*' | grep -e '(^1:)|syslog' | cut -d ':' -f2-
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
message+     567  0.0  0.0  12928  9928 ?        Ss   5月07   1:22 /usr/bin/dbus-daemon --system --address=systemd
syslog       597  0.0  0.0 224332  4960 ?        Ssl  5月07   0:07 /usr/sbin/rsyslogd -n -iNONE
int4      298295  0.0  0.0  17676   736 pts/1    S+   14:39   0:00 grep --color=auto -e (^1:)|syslog

原文地址:https://www.cnblogs.com/amize/p/14982124.html