Shell Primer-grep Filter-1

Source: Internet
Author: User
Tags gopher

The regular expression is a string. There is a certain pattern. We use the specified string to match a specified line. The specified string is the regular expression.

Regular expressions have these tools: grep egrep sed awk

Command: Gerep

Description: Filters out the specified rows

Option:--color keywords have color

-N Display Line number

-C shows how many rows have appeared altogether

-V takes a row that does not contain the specified character

-a n n refers to a number, such as A2, which displays two lines below the line with the specified character

-B n n refers to a number such as B2 to display two lines above the line with the specified character

-c n n refers to numbers such as C2 the lines above and below the line with the specified characters.

-R Displays the rows in the directory with the specified characters

-RH display in the directory the line with the specified character does not display the file path and filename

grep filters out the rows with root

[[email protected] ~]# grep--color ' root '/etc/passwd
root: x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

Grep-n

[[email protected] ~]# grep-n ' root '/etc/passwd
1:root:x:0:0:root:/root:/bin/bash
11:operator:x:11:0:operator:/root:/sbin/nologin

Create an individual name for grep and copy the/etc/passwd

[[email protected] ~]# vim. BASHRC

Alias cg= ' grep--color '

[Email protected] ~]# cp/etc/passwd 1.txt
CP: Do you want to overwrite "1.txt"? Y

-C

[[email protected] ~]# cg-c ' root ' 1.txt
2

-V

[[email protected] ~]# cg-v ' root ' 1.txt
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Adm:x:3:4:adm:/var/adm:/sbin/nologin
Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
Sync:x:5:0:sync:/sbin:/bin/sync
Shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

....

-A

[[email protected] ~]# cg-n-A 2 ' root ' 1.txt
1:root: x:0:0:root:/root:/bin/bash
2-bin:x:1:1:bin:/bin:/sbin/nologin
3-daemon:x:2:2:daemon:/sbin:/sbin/nologin
--
11:operator:x:11:0:operator:/root:/sbin/nologin
12-games:x:12:100:games:/usr/games:/sbin/nologin
13-gopher:x:13:30:gopher:/var/gopher:/sbin/nologin

-B

[[email protected] ~]# cg-n-b1 ' root ' 1.txt
1:root: x:0:0:root:/root:/bin/bash
--
10-uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
11:operator:x:11:0:operator:/root:/sbin/nologin

-C

[[email protected] ~]# cg-n-c1 ' root ' 1.txt
1:root: x:0:0:root:/root:/bin/bash

2-bin:x:1:1:bin:/bin:/sbin/nologin
--
10-uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
11:operator:x:11:0:operator:/root:/sbin/nologin
12-games:x:12:100:games:/usr/games:/sbin/nologin

-R

[[email protected] ~]# cg-r ' root ' ~

.....

/root/.bash_history:home=/root
/root/.bash_history:grep ' Root '/etc/passwd
/root/.bash_history:grep-n ' Root ' 1.txt
/root/.bash_history:grep-c ' Root ' 1.txt
/root/.bash_history:grep-v ' Root ' 1.txt
/root/.bash_history:grep-a2 ' Root ' 1.txt
/root/.bash_history:grep-n-a2 ' root ' 1.txt
/root/.bash_history:grep-n-b2 ' root ' 1.txt
/root/.bash_history:grep ' Root '/etc/passwd
/root/.bash_history:grep-n ' Root '/etc/passwd
/root/.bash_history:cg-c ' Root ' 1.txt

.......

[[email protected] ~]# cg-rh ' root ' ~

....

Home=/root
grep ' Root '/etc/passwd
Grep-n ' Root ' 1.txt
Grep-c ' Root ' 1.txt
Grep-v ' Root ' 1.txt
Grep-a2 ' Root ' 1.txt
Grep-n-a2 ' root ' 1.txt
Grep-n-b2 ' root ' 1.txt
grep ' Root '/etc/passwd
Grep-n ' Root '/etc/passwd
Cg-c ' Root ' 1.txt

......

//////////////////////////////////////////////////////////////////////////

Summary: Vim ~/.BASHRC//alias cg= ' grep--color '

Cg-n-v-c-R-RH-AN-BN-CN

Shell Primer-grep Filter-1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.