grep, sed, awk Three Musketeers

grep

Text content filtering

usage:

1.grep -i case-insensitive

[root@s120 ~]# echo -e "hello world\nHELLO world"|grep -i hello
hello world
HELLO world
[root@s120 ~]# echo -e "hello world\nHELLO world"|grep hello
hello world

2.grep -n display line numbers

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

3.grep -o print only the content that matches

[root@s120 ~]# echo "hello world"|grep -o hello
hello
[root@s120 ~]# echo "hello world"|grep hello
hello world

4.grep -c print only the number of rows that match

[root@s120 ~]# grep -c root /etc/passwd
2
[root@s120 ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

5.grep -v print lines that do not match

[root@s120 ~]# echo -e "hello world\nyou are beautiful"|grep -v hello
you are beautiful
[root@s120 ~]# echo -e "hello world\nyou are beautiful"|grep hello
hello world

6.grep -w exact match

[root@s120 ~]# echo -e "hello world\nhellow world"|grep hello
hello world
hellow world
[root@s120 ~]# echo -e "hello world\nhellow world"|grep -w hello
hello world

and

A non-interactive editing tools

usage:

CRUD

By 
adding a append after row 
i into the front insert rows 
. 1  Sed  " . 2A Hello " Test or Sed . 2A " Hello " Test 2 Sed " 2i Hello " Test
 . 3 Sed " 2i Hello \ nworld \ nBeautiful " before the second line inserting multiple test # OK
 puncturing 
d delete delete 1 Deletes the specified row
 Sed ' 2D ' Test
 2 deletes the specified range of row
 Sed ' 2,5d ' Test
 . 3 removes matching rows
 Sed ' 
    
/ Sixth / D ' Test . 4 line to the last line to delete the specified content Sed ' 2, D $ ' Test . 5 negated (retaining only) Sed ' 2,3! D ' Test # lines retain only 2,3 Sed ' / tenth /! D ' Test # keep only the tenth line containing sed ' / the root /! D ' / etc / the passwd equal to the root grep / etc / the passwd change c change replace sed ' 2C Hello ' Test # replacing the second row sed replacement software model . 1 Sed ' S / Line / Hang / G ' Test 2 Sed-i ' S / Line / Hang / G ' the Test Command Description: If you really want to modify the contents of the documents, we need to use the option " - i", and this should sed command "i" separate. At the same time we can see the results after the command is executed without any output.
Charles p print printout specify the content, but the default output will match two times, so using
- the n-output option to cancel the default sed '2p' / etc / passwd entire contents of the file # display, the second line will be shown twice sed - n-'2P' / etc / the passwd or Sed ' ! D 2 ' / etc / the passwd # only the second line Sed -n ' / ninth / P ' Test # displays only the ninth row - E multipoint operation Sed -e ' 2d '-e ' 5D ' Test # 5 and delete lines 2 Sed -n -e ' 2P ' -e ' 5P ' Test # 5 and shows only 2 lines

awk

Gets the ranks of the required information

concept:

-F: specify the delimiter

Record (record): a row is a record

Separator (field separator): character when cutting the recording to be used

Field (field): The record of each segment is divided into

FILENAME: current processing file name

FS (Field Separator): Field Separator (default = delimiters are space)

NR (Number of Rrecord): record number (read every line awk, NR is incremented == 1)

NF (Number of Field): number of fields (current record This record contains the number of fields ==)

ORS (Output Record Separator): Specifies the output record separator (specify what records in the output end, the default is the \ n, i.e. newline)

OFS (Output Field Separator): Output Field Separator

RS: record separator

usage:

$ 1 $ 2 ... $ n  fields (columns) outputs a specified first . 1 column, the second 2 columns, the first n columns

$ NF  output of the last field

$ 0  output entire record

1 to print all of the content display line numbers
 awk  ' {Print NR, $ 0} ' / etc / the passwd 
2 outputs over five fields of the third field line    
 awk -F ' : '  ' of NF> =. 5 {Print $. 3} ' / etc / the passwd 
. 3 output and the row number of each row have several fields
 awk -F ' : '  ' {Print NR, of NF} ' / etc / the passwd 
. 4 matches the first line of the root 
awk  ' / ^ the root / ' / etc / the passwd 
. 5 match the end of a line of Sync
 awk -F ' : '  ' / Sync $ /' / Etc / the passwd 
. 6 matches a column in a row
 awk -F ' : '  ' $ 5 ~ / root / ' / etc / the passwd         # 5 as line matching the root 
7 shows a fourth row 
awk  ' NR. 4 == ' / etc / the passwd 
. 8 printed before the results hello, may be any character
 awk  ' the BEGIN {Print "hello"} {Print $ 0} ' / etc / the passwd 
. 9 printed hello after a result, may be any character
 awk  ' the END { Print "Hello" Print $ {0}} '/ etc / the passwd 
10 using the calculated awk + Addition - Subtraction * multiplication / addition can take () ^ prescribing
 awk  '* 2. 3 {Print the BEGIN} '  
. 11 Comparative> greater than <less than> = Greater than or equal to <= Less than or equal == equal
 awk -F " : "  ' $. 3 <= 88. 1 {Print $, $. 3} ' / etc / the passwd

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/qianjisan66/p/11590470.html