how to extract specific value using grep and awk

2019-09-21 10:12发布

问题:

Im using cat /etc/inetd.conf | grep '#'chargen and I receive

#chargen        stream  tcp     nowait  root    internal
#chargen        dgram   udp     wait    root    internal

how I can extract only once word #chargen from this output:

#chargen

回答1:

Could you please try following.

awk '/^#chargen/{print $1; exit}' Input_file

or

grep -m1 -o '#chargen' /etc/inetd.conf


标签: awk grep