One, grep: Text Search Tool
-C output only matches the number of rows
-I is case insensitive
-V filters out rows containing the specified string
-S does not display error messages for nonexistent or no matching text
-W displays only the entire word that matches, not part of the string
-N Displays matching lines and line numbers
-l list only matching filenames
-l list file names that do not match
Cases:
grep-c root/etc/passwd if the containing root string has two lines in the file, it is displayed as 2
grep-i root/etc/passwd the line that displays the root string, including its case
grep-v root/etc/passwd Show rows with root string except
grep ' test ' d* shows all lines that contain the test string in a file that begins with D
grep ' ^$ '/etc/passwd show Blank lines in the passwd file
grep ' test ' AA bb cc displays the line that matches test in the aa,bb,cc file
The main parameters of the regular expression:
\ ignore the original meaning of special characters in regular expressions
[] A single character, such as [a] that [a] meets the requirements
[-] range, such as [A-z], i.e. a,b,c to Z all meet the requirements
grep ' [a-z]\{5\} ' AA displays all lines containing a string of at least 5 consecutive lowercase characters per string, A-Z can
grep ' w\ (es\) t.*\1 ' AA if West is matched, then es is stored in memory, labeled 1, and then searched for any character (. *) followed by another ES (\1), which is found to display the row. Example: west.es west.esdd.es west.aa.es
grep test/mnt/* directory search for files with string test
grep-l test/mnt/* lists only matching file names
grep-l test/mnt/* list file names that do not match
[[: Alpha:]] single letter
[[: Lower:]] single lowercase letter
[[: Upper:]] single capital Letter
[[: Alnum:]] single letter or number
[[:p UNCT:]] single symbol with no numbers, letters, and spaces
[[:d igit:]] number
[[: Space:]] space
Example: grep ' 5[[:upper:]][[:upper:] ' AA displays lines beginning with 5 with two uppercase letters
ii. sed: Text Processing tools
-U Show modified text content, but not really change
-I directly modifies the file without displaying the content
Sed ' 1d ' file delete first line
Sed ' 2, $d ' file delete the second line to the last row
Sed ' 1a play lol ' file after the first line adds the string "Play LOL"
Sed ' 5i go die ' file added "Go die" before line 5th
SED ' 1c hi ' file first line instead of hi
Sed-n 2, ' $ ' p file shows the second line to the last row
Sed ' s/^.*asd.*$/lizekang/g ' file matches the ASD character and modifies the integer behavior Lizekang
Sed ' 14c [file] ' file modified 14 line integer behavior [file]
Sed-n '/^\//p ' file displays lines with/starting (if no-n option is present, all content in file and/or lines starting with two parts are displayed, N-mode filters out other rows, only displays/starts rows)
Sed '/^$/d ' fiel delete blank lines in a file
Sed '/^#/d ' file deletes the # number at the beginning of the document
Sed ' s/^[[:spqce:]]*//g ' fiel delete spaces at the beginning of a file
Third, awk: Text analysis tools
-F (split word): Do not add this parameter by default separated by a space.
' {print $} ': Required field, output the specific paragraph, $NF represents the last element.
awk ' {print nr,$0} ' is preceded by a line number in front of each line.
awk ' {if (nr<=3+1 && nr>=2) print $} ' outputs the second line to line fourth.
Netstat-an|awk '/^tcp/{++state[$NF]} END {for (I in State) print I, "\ T", state[i]} '
/^tcp/filter out the records of the beginning of TCP, shielding Udp,socket and other unrelated records.
State[] equivalent to defining an array called state
NF indicates the number of fields recorded
State[$NF] Represents the value of an array element, such as the number of connections on state[time_wait]
++state[$NF] Number of connections above plus 1
Here are some of awk's exercises:
1. Print the entire test.txt with awk (the following operations are implemented with the awk tool for Test.txt);
awk ' {print $} ' test.txt
2. Find all rows that contain ' bash ';
awk '/bash/' test.txt
3. Use ': ' as a delimiter to find the line with the third paragraph equal to 0;
awk-f ': ' $3== ' 0 "' Test.txt
4. Use ': ' as a delimiter to find the first line of ' root ' and replace that segment ' root ' with ' Toor ' (which can be used together with SED);
awk-f ': ' $1== ' root ' test.txt | sed ' s/root/root/g '
5. Use ': ' as a separator to print the last paragraph;
awk-f ': ' {print $NF} ' Text.txt
6. Print all rows with a number of rows greater than 20;
awk-f ': ' nr>20 ' Test.txt
7. Use ': ' As a delimiter to print all third paragraphs less than the fourth paragraph;
awk-f ': ' $3<$4 ' Test.txt
8. Use ': ' As a separator, print the first paragraph and the last paragraph, and the middle with ' @ ' connection (for example, the first line should be the form of "[Email Protected]/bin/bash";
awk-f ': ' {print $ ' @ ' $NF} ' test.txt
9. Use ': ' As a delimiter to add and sum the fourth paragraph of the entire document;
awk-f ': ' {(sum+=$4)}; END {print sum} ' test.txt
This article is from the "11721816" blog, please be sure to keep this source http://zzkkk.blog.51cto.com/11721816/1846979
The Three Musketeers of Linux Foundation