cmdref.net - Cheat Sheet and Example

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

User Tools

Site Tools


Sidebar








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.

Example

Deleting comments of a configuration

# grep -Ev "^#|^$" 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-*'.

# 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
ifcfg-eth1:TYPE=Ethernet
ifcfg-eth1:MASTER=bond1
ifcfg-eth1:SLAVE=yes
ifcfg-eth1:USERCTL=no
ifcfg-eth2:E=eth2
ifcfg-eth2:BOOTPROTO=none
ifcfg-eth2:ONBOOT=no
(abbr)


recursive

grep -r PATTERN  DIRECTORY
grep -r PATTERN  DIRECTORY
grep -r PATTERN  --include="*.txt" DIRECTORY
grep -r final  dir
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: 2018/02/09 by admin

Page Tools