cmdref.net - Cheat Sheet and Example

cmdref.net is command references/cheat sheets/examples for system engineers.

User Tools

Site Tools


Sidebar








Cloud



Etc


Reference














.

os:linux:command:grep.html



Linux Commands#File, Directory

grep - How to use grep command in Linux with examples

OPTIONS

       -i, --ignore-case
              Ignore case distinctions in both the PATTERN and the input files.

       -E, --extended-regexp
              Interpret PATTERN as an extended regular expression (see below).

       -v, --invert-match
              Invert the sense of matching, to select non-matching lines.

       -A NUM, --after-context=NUM
              Print  NUM  lines of trailing context after matching lines. 
              Places a line containing -- between contiguous groups of matches.

       -B NUM, --before-context=NUM
              Print NUM lines of leading context before matching lines. 
              Places a line containing -- between contiguous  groups  of matches.

       -R, -r, --recursive
              Read all files under each directory, recursively; this is equivalent to the -d recurse option.

        -H, --with-filename       print file name with output lines

Example

Deleting comments of a configuration

# grep -Ev "^#|^$" /etc/ssh/sshd_config
Protocol 2
SyslogFacility AUTHPRIV
PasswordAuthentication yes
ChallengeResponseAuthentication no
GSSAPIAuthentication yes
GSSAPICleanupCredentials yes
UsePAM yes
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
X11Forwarding yes
Subsystem       sftp    /usr/libexec/openssh/sftp-server
#


# grep -Ev "^*#|^$" /etc/httpd/conf/httpd.conf

# grep -Ev "^$|^#|^\s*#"  /etc/httpd/conf/httpd.conf

# grep -Ev '^[ ^t]*#|^$'  /etc/httpd/conf/httpd.conf


you should use head -n 10 ifcfg* |more insted of grep . ifcfg-* or grep "" ifcfg-*.

  -H, --with-filename       print file name with output lines
# grep . ifcfg-*
ifcfg-eth0:TYPE="Ethernet"
ifcfg-eth0:BOOTPROTO="dhcp"
ifcfg-eth0:DEFROUTE="yes"
ifcfg-eth0:PEERDNS="yes"
ifcfg-eth0:PEERROUTES="yes"
ifcfg-eth0:IPV4_FAILURE_FATAL="no"
ifcfg-eth0:IPV6INIT="yes"
ifcfg-eth0:IPV6_AUTOCONF="yes"
ifcfg-eth0:IPV6_DEFROUTE="yes"
ifcfg-eth0:IPV6_PEERDNS="yes"
ifcfg-eth0:IPV6_PEERROUTES="yes"
ifcfg-eth0:IPV6_FAILURE_FATAL="no"
ifcfg-eth0:NAME="eth0"
ifcfg-eth0:DEVICE="eth0"
ifcfg-eth0:ONBOOT="yes"
ifcfg-lo:DEVICE=lo
ifcfg-lo:IPADDR=127.0.0.1
ifcfg-lo:NETMASK=255.0.0.0
ifcfg-lo:NETWORK=127.0.0.0
(abbr)
# grep "" ifcfg-*
ifcfg-bond0:DEVICE=bond0
ifcfg-bond0:BOOTPROTO=static
ifcfg-bond0:ONBOOT=yes
ifcfg-bond0:IPADDR=172.16.xx.xx
ifcfg-bond0:NETMASK=255.255.255.0
ifcfg-bond0:BONDING_OPTS="mode=1 miimon=100 updelay=500"
ifcfg-bond1:DEVICE=bond1
ifcfg-bond1:BOOTPROTO=static
ifcfg-bond1:ONBOOT=yes
ifcfg-bond1:IPADDR=10.50.x.xx
ifcfg-bond1:NETMASK=255.255.255.0
ifcfg-bond1:BONDING_OPTS="mode=1 miimon=100 updelay=500"
ifcfg-eth0:DEVICE=eth0
ifcfg-eth0:BOOTPROTO=none
ifcfg-eth0:ONBOOT=yes
ifcfg-eth0:TYPE=Ethernet
ifcfg-eth0:MASTER=bond0
ifcfg-eth0:SLAVE=yes
ifcfg-eth0:USERCTL=no
ifcfg-eth1:DEVICE=eth1
ifcfg-eth1:BOOTPROTO=none
ifcfg-eth1:ONBOOT=yes
(abbr)


recursive

grep -r PATTERN  DIRECTORY
grep -r PATTERN  --include="*.txt" DIRECTORY
grep -r final  dir

grep -r -i http_proxy  /etc   2> /dev/null
grep -r hogehoge /var/www/html
grep -r hogehoge --include="*.html" /var/www/htm

find . -name '*.java' | xargs grep 'final'


Before / After context

$ cat test.txt
1
2
3 aaa
4
5
6
$

$ grep -2 aaa test.txt
1
2
3 aaa
4
5
$

$ grep -A 2 aaa test.txt
3 aaa
4
5
$


$ grep -B 2 aaa test.txt
1
2
3 aaa
$



os/linux/command/grep.html.txt ยท Last modified: 2024/07/26 by admin

Page Tools