1. grep command: Find the matching strings in the file
A powerful text search tool that uses regular expressions to search for text and print matching rows.
Syntax format: grep [options]
-I: ignore-case ignores case differences
-C: count: only prints the total number of matched rows and does not display the Matching content.
-N: line-number: print the row number before the matched row
-V: revert-match: Only unmatched rows are displayed.
-R: recursion recursively reads all files in each folder.
-L: displays only the names of the matching files instead of the common output results.
2. awk command: A Powerful text processing tool that scans lines by line from the first line to the last line
Syntax: awk 'pattern {action} 'filename
Pattern: Regular Expression
Action: Output syntax
You can omit either pattern or action, but not both. If pattern is omitted, no style match exists, indicating that all rows (Records) are operated, if action is omitted, the default operation is executed. The default operation is displayed on the standard output.
Syntax format: awk [-F re] [parameter...]
-F re: Allows awk to change its field separator
Parameter: this parameter helps assign values to different variables.
-V: defines variables.
-F: Specifies the script file.
Three call methods:
1. awk command line
2. Use the-f option to call the awk program, for example, awk-f progfile file, where progfile specifies a text file
3. To use the command interpreter to call the awk program, you must declare the call method in the awk script, for example :#! /Bin/awk-f
Use the following command line to filter content:
[Root @ test ~] # Awk '/root/'/etc/passwd
Root: x: 0: 0: root:/bin/bash
Operator: x: 11: 0: operator:/root:/sbin/nologin
# Specify the column (column 1) for the display separator. The default Delimiter is space and $1 is the column before the display separator.
# Display rows matching 123 in the test file
[Root @ test ~] # Awk '/123/{print $1}' test
# Display all rows starting with a or f
4. sed command: an online editor that processes a line of content at a time
During processing, the currently processed rows are stored in the temporary buffer, called 'pattern space'. Then, the sed command is used to process the content in the buffer, send the buffer content to the screen. Next, process the next row, and repeat until the end of the file. The file content is not changed unless you use the redirection storage output.
Common parameters:
I insert
S replacement
E multi-point editing
D. Delete
A is appended to the end of the row.
Replace all with g, and replace only the first one in each line with no g
Q exit
# Replace all contained aaa with jjj
Sed's/aaa/jjj/'test
# Remove this line of comment and replace the text
Sed-I's/# ServerName www.example.com: 80/ServerName 192.168.0.202: 80/G'
Replace/var/www/html/with/opt/web/. The separator '/' can be replaced by other symbols, such as ',' _ '| '.
Sed-I s/\/var \/www \/html/\/opt \/web/'/etc/httpd/conf/httpd. conf
Equivalent:
Sed-I's _/var/www/html/_/opt/web/_ '/etc/httpd/conf/httpd. conf
# Insert a row to 391 rows, including the special symbol '/'
Sed-I '100 s/^/AddType application \/x-httpd-php .php.html/'httpd. conf
# Print only the first row of data
Sed-n '1p'/etc/passwd
# Print the row containing root and disable default output
Sed-n'/root/P'/etc/passwd
# Delete 13 rows
Sed-I '13d'/etc/passwd
# Delete 13 rows to the last row
Sed-I '13, $ D'/etc/passwd
# Delete rows containing uucp
Sed-I '/UCP/D'/etc/passwd
5. find command: find Files with certain characteristics (such as file permissions, file owner, file length, and file type)
Usage: find [path] [options] [condition]
Path: Search Path
Options: Options
For example:
-Name: searches for files by file name
-Mtime: searches for the file based on the file change time, followed by-n and + n to indicate the number of days and the previous
-User: searches for files based on the file owner
-Group: searches for files according to the group to which the files belong.
-Type: searches for files of a certain type, such as B (Block device), d (directory), l (symbolic link), and f (common file)
-Size: searches for files based on the file size.
# Search for files whose modification time is within 5 days in the root directory
Find/tmp-mtime-5
# Search for files whose modification time is earlier than 3 days in the/var directory
Find/var/-mtime + 3
# Search for common files with a length of 0 in the system and list their full paths
Find/-type f-size 0-exec ls-l {}\;
# Find common files whose change time is earlier than 7 days in the/var/log directory and delete them
Find/var/log/-type f-mtime + 7-exec rm {}\;
# Find the Files Owned by the user test and copy them to the/root/test directory.
Find/-user test-exec cp {}/root/test \;
6. sort command: sort the rows in the file
The sort command sorts the content in the file line by line. If the first character of the two lines is the same, the command will continue to compare the next character of the two lines, the comparison will continue.
Format: sort [Option] File
Main options:
-R: sort in descending order. The default value is ascending.
-N: sort by value
-K: KeyDefinition specifies the sorting keyword.
-T: Character specifies Character as the field separator
-D: sort in alphabetical order. Only letters, numbers, and spaces are considered for comparison.
-F: Treat lowercase letters as uppercase letters
-U: removes duplicate rows to make each row in the file unique.
7. uniq command: delete duplicate lines in the file
After the file is processed, duplicate rows may appear in its output file. For example, if you use the cat command to merge two files and then use the sort command to sort the files, duplicate lines may appear. In this case, you can use the uniq command to delete these repeated rows from the output file, leaving only unique samples for each record.
Format: uniq [Option] File
Main options:
-C: displays the row number.
-D: only duplicate rows are displayed.
-U: only show the rows in the file that are not repeated.
8. cut command: display the specified content of each row in the file
Format: cut-d delimiter [-cf] fields
-D: delimiter followed by a Tab by default;
-C: followed by the nth character
-F: The blocks to be followed
# List all users in the/etc/passwd file in ascending order
Cut-d:-f 1/etc/passwd | sort
# Display the username and uid with uid 0 in the/etc/passwd file.
Cut-d:-f 1, 3/etc/passwd | grep ': 0 $'
The regular expression '$' indicates the end of a character, and '^' indicates the start of a character.
9. tr command: delete or replace characters from standard input. It can be seen as sed simplified software.
The tr Command Format of common options is:
-D: delete all input characters in string 1.
-S deletes all recurring character sequences and retains only the first one.
# Convert lowercase characters to uppercase letters
Tr 'a-Z' <file
# Delete All null characters from the file
Tr-d '\ 0' <file
# Deleting shell characters in an object
Cat test | tr-d 'shell'
# Replace efg with all abc characters in the file and save it as a new file
Cat test | tr 'abc' 'efg'> new file
10. other text processing commands
Cat: displays content from the beginning and outputs all content
Common parameter.-n indicates the number of the output rows.
Tac: displays content in reverse order from the last line and outputs all content
Head: by default, the first 10 rows are displayed.-n indicates the number of rows to be displayed.
Tail: by default, the last 10 rows are displayed.-n specifies the number of rows to be displayed.-f displays the content in real time.
More: view text files on a split screen
Less: similar to more, but you can flip the page forward
Nl: output line number when displaying
Wc: number of bytes, number of words, and number of lines of computing files
Common parameters:-c,-l, and-m