1. Regular expressions
^ |
Line Start flag |
$ |
End of line Mark |
. |
Match any one character |
[ ] |
Match any one of the characterscontained in [ character ] ,Coo[kl] matches Cook or Cool |
[^] |
Match any one character except [^ character ] |
[-] |
Matches any one of the characters in the range [] |
? |
Match previous item once or 0 times |
+ |
Match a previous item one or more times |
* |
Match previous Items 0 or more times |
() |
Create a substring for matching |
N |
Matches the previous item n times |
{N,m} |
Specifies the minimum and maximum number of times the previous item must match |
| |
Alternate - match | any one of the two sides |
\ |
Escape character to escape the above symbol |
2.grep
(1) Search for lines of text that contain a specific pattern
Cat 1 . txt this is a test2 One - - - - grep3 1 You can also search for multiple files:grep 3 1 2 3 . txt using regular expressions must use the Egrep
(2) To print all rows except a row
grep 3 1 . txt this is a test2 One - - -
(3) The number of lines in the statistics text or text containing matching strings
grep 3 1 1
(4) Print the number of lines that contain matching strings
grep 3 1 4:
(5) Search multiple files and find out which file the matching text is in
grep 3 1 2 1.txt2. txt
(6) If you are querying recursively, you can use the
grep "Text". -r-n
(7) Ignore case in style (grep-i pattern file)
(8) match multiple styles with grep
grep -E "pattern1"-E "pattern2"
(9) Specifying or excluding files in grep search
grep "Main ()". -R--exclude "Readne"
(10) Print out lines before or after matching text
To print 3 rows after matching a result, you can use the-a option
seq Ten| grep 5 3 5 6 7 8
To print 3 rows before matching a result, you can use the-B option
seq Ten| grep 5 3 2 3 4 5 to print 3 rows before and after matching a result, use -seq| grep 5 3 2 3 4 5 6 7 8
3. Splitting text by column with cut
Cut 2,3 filename
4.sed
(1SED can replace strings in the given textsed' S/pattern/replace_string 'file(2By default, SED will print only the replaced text, and if you want to keep the changes, use-i optionsed-I. ' S/pattern/replace_string 'file(3If you want to replace all content, add the parameter G at the endsed' S/pattern/replace_string/g 'file(4) Remove Blank linessed'/^$/d 'file(5) is replaced directly in the filesed' S/pattern/replacement '-I filename (6) combine multiple expressionssed' Expression ' |sed' expression '=sed' Expression;expression '=sed-E ' expression '-e ' expression '
5.awk
the basic structure of the script is as follows: awk file awk ' BEGIN {statements} {statements} END {END statements} '
6. Statistical frequency in specific documents
#!/bin/bash# Use: Calculates the word frequency of words in a fileif[$#-ne1 ]; Then Echo "usage:$0 filename"; Exit-1fifilename=$1Egrep-O"\b[[:alpha:]]+\b"$filename | awk '{count[$0]++}end{printf ("%-14s%s\n","Word","Count"); for(Indinchcount) {printf ("%-14s%d\n", Ind,count[ind]); }}'~[email protected] shell]#SHWord_freq.SH/tmp/1. txt Word counta1 This1 is1
7. Merging multiple files by column (paste)
1 2 . txt is a test2 cairui xijinpng Aiyinsitan about
8. Print the nth Word or column in a file or line
Print the 5th column below awk ' {print $5 } ' filename
9. Print the text between lines or styles
(1) [[email protected] tmp]# seq 100|awk ' nr==4,nr==8 '
(2) 4awk ' nr==m,nr==n ' filename
5
6
7
8
shell-4-Let the text fly