Home > OS >  Linux under the gerp explanation
Linux under the gerp explanation

Time:10-29

Introduction to

Grep (global search regular expression (RE) and print out the line, a comprehensive search regular expression and print line) is a powerful text search tool, it can use regular expressions to search text, and the matching line printed,

Unix grep family including grep, egrep and fgrep egrep and fgrep command only have small different with grep, egrep is an extension of the grep, support more re metacharacters, fgrep is fixed grep or fast grep, they consider all the letters of the word, that is to say, in the regular expression metacharacters said back to its literal meaning, no longer special, Linux USES the GNU versions of grep, it more powerful, can pass - G, E, -f command line options to the function of the use of egrep and fgrep,



Grep common usage


Copy the code
[root @ WWW ~] # grep [- acinv] [-- color=auto] 'search string filename
Options and parameters:
- a: binary file search data in the form of text file
- c: calculates the number of finding 'search string'
- I: ignore case is different, so the same case as
- n: by the way, the output line number
- v: reverse choice, which shows no 'search string content that line!
-- color=auto: can add a color display will find keywords part!

Copy the code


The/etc/passwd and have a line out of the root


Copy the code
# grep root/etc/passwd
Root: x: 0-0: root:/root:/bin/bash
Operator: x: 11:0: operator:/root/sbin/nologin:
Or
# cat/etc/passwd | grep root
Root: x: 0-0: root:/root:/bin/bash
Operator: x: 11:0: operator:/root/sbin/nologin:

Copy the code



The/etc/passwd, root out line, at the same time show the walk in the number of rows in the/etc/passwd

# grep - n root/etc/passwd
1: root: x: 0-0: root:/root:/bin/bash
30: operator: x: 11:0: operator:/root/sbin/nologin:

In the display of the keyword, grep can use -- color=auto to use color display of key part, this is a very nice feature! But if every time using grep to itself and again -- color=auto show very troublesome ~ at this point the use alias is derived with the! You can be in ~/. Add this line: (alias in bashrc grep='grep -- color=auto) to (source ~/. Bashrc) to take effect immediately! So that each run grep he will automatically help you add color display



The/etc/passwd and will not appear line out of the root

# grep -v root/etc/passwd
Root: x: 0-0: root:/root:/bin/bash
Operator: x: 11:0: operator:/root/sbin/nologin:



/etc/passwd, no root and nologin line out

# grep -v root/etc/passwd | grep -v nologin
Root: x: 0-0: root:/root:/bin/bash
Operator: x: 11:0: operator:/root/sbin/nologin:



Listed in dmesg core information, and then to grep to find it contains the eth that line, to catch the key words color, and add a line number to indicate:

/root @ WWW ~ # dmesg | grep - n -- color=auto 'eth'
247: eth0: RealTek RTL8139 at 0 xee846000, 00:9 0: cc: a6: azaliah, 4 IRQ 10
248: eth0: Identified 8139 chip type 'RTL - 8139 - c'
294: eth0: link up, 100 MBPS, full - duplex, lpa 0 xc5e1
305: eth0: no IPv6 routers present
# you will find that, besides the eth have special colors to represent the front there is a line number!

In the display of the keyword, grep can use -- color=auto to use color display of key part, this is a very nice feature! But if every time using grep to itself and again -- color=auto show very troublesome ~ at this point the use alias is derived with the! You can be in ~/. Add this line: (alias in bashrc grep='grep -- color=auto) to (source ~/. Bashrc) to take effect immediately! So that each run grep he will automatically help you add color display



Listed in dmesg core information, and then to grep to find it contains the eth that line, after the key word in the lines of the first two lines and three lines to catch up with also shows


Copy the code
/root @ WWW ~ # dmesg | grep - n - A3 - B2 -- color=auto 'eth'
245 - PCI: setting IRQ 10 as level - triggered
0000:246 - ACPI: PCI interrupts 00:0 e. 0 [A] - & gt; The Link [LNKB]...
247: eth0: RealTek RTL8139 at 0 xee846000, 00:9 0: cc: a6: azaliah, 4 IRQ 10
248: eth0: Identified 8139 chip type 'RTL - 8139 - c'
249 - input: PC Speaker as/class/input/input2
0000:250 - ACPI: PCI interrupts 00:01onsaturday (UK time). 4 [B] - & gt; The Link [LNKB]...
48 x DVD 251 - HDB: ATAPI cd-rom DVD - R - RAM cd-r/RW drive, 2048 KB Cache, UDMA (66)
# as shown above, you will find that the keyword in the first two lines of 247 and 248 after three lines are displayed!
# so I can let you to keyword analysis data before and after the catch out!

Copy the code



According to the file content recursive search directory

* # # grep 'energywise files in the current directory search with' energywise 'line

# grep -r 'energywise * # under the current directory and its subdirectories search' energywise 'the file
# grep -l - r 'energywise * # under the current directory and its subdirectories search' energywise 'lines of the file, but does not display the matching line, show only the matches the file

This a few command is used, is to find files,



The grep with regular expression

The character class

Character class search: if I want to search for the test or taste, these two words, can be found, in fact they have in common 't? St 'existence ~ this time, I can search for it this way:

/root @ WWW ~ # grep - n 't (ae) st' regular_express. TXT
8: I can 't finish the test.
9: Oh! The soup taste good.


Actually [] whether there are several bytes, he is on behalf of a (a) bytes, so the above example shows, I need to string is (tast) or two (test) string!



Character classes of reverse choice [^] : if you want to search to oo a line, but don't want the oo g ahead, following

/root @ WWW ~ # grep - n '] [^ g oo 'regular_express. TXT
2: the apple is my favorite food.
3: Football game is not use feet only.
18: Google is the best tools for the search keyword.
19: goooooogle yes!

2, 3 lines without doubt, because foo and foo are acceptable!

But line 18 have Google's goo ~ don't forget, because the bank behind the tool too! So the bank has also been listed - that is, 18 inside despite what we don't want project (goo) that), but due to the need of project, therefore, is in line with the string to search for!

As for line 19, the same, because goooooogle may be in front of the oo o, for example: go (ooo) Google, so, this line is also in line with the requirements of!



Continuous character classes: again, if I in front of the oo don't want to have a small bytes, so I can write that [z] ^ the abcd... oo, but this seems to be not very convenient, because in the small bytes of ASCII code sequence is continuous, so we can simplify it to under this:

/root @ WWW ~ # grep - n '[^ a-z] oo' regular_express. TXT
3: Football game is not use feet only.

That is to say, when we are in the collection of a group of bytes, if the byte group is continuous, uppercase/lowercase English/digital, and so on, for example, you can use [a-z], [a-z], [0-9] to writing, so if we request string is digital and English? Ha ha! Will he write all together, became: [a - zA - Z0-9],

We want to get the line Numbers, like this:

/root @ WWW ~ # grep - n '[0-9] regular_express. TXT
5: clear that this dress is about $3183 dollars.
15: You are the best is mean You are the no. 1.



nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related