Content directory: Find file lookup grep text Search xargs command line parameter conversion sort sort Uniq eliminate duplicate rows with TR convert cut by column split text paste by column stitching text WC statistic lines and characters tool sed text substitution benefits The lines, words, and characters in the iteration file for the AWK Data flow processing tool
This article describes the most commonly used tools for using shell to process text under Linux:
Find, grep, Xargs, Sort, uniq, tr, cut, paste, WC, sed, awk;
The examples and parameters provided are the most common and practical;
The principle I use for Shell scripting is to write the command line, and try not to exceed 2 lines;
If you have more complex task requirements, consider Python; find File Lookup
Find txt and PDF files
Find. \ (-name "*.txt"-o-name "*.pdf" \)-print
Find txt and PDF in a regular way
Find. -regex ". *\ (\.txt|\.pdf\) $"
-iregex: Ignores case-insensitive regular
Negative parameters
Find all non-txt text
Find. ! -name "*.txt"-print
Specify Search Depth
Print out the current directory file (depth 1)
Find. -maxdepth 1-type F
Custom Search
Search By Type:
Find. -type d-print //Lists all catalogs only
-type f File/L Symbolic link
Search by Time:
-atime Access time (units are days, minutes are-amin, similar to the following)
-mtime modification Time (content modified)
-ctime change time (meta data or permission changes)
All files that have been visited in the last 7 days:
Find. -atime 7-type F-print
Search by Size:
W Word k M G
Looking for files larger than 2k
Find. -type f-size +2k
Find by Permission:
Find. -type f-perm 644-print//Find all files with executable permissions
Find by User:
Find. -type f-user weber-print//to find files owned by user Weber
follow-up action after finding
Delete:
Delete all SWP files in the current directory:
Find. -type f-name "*.SWP"-delete
Execute action (Mighty exec)
Find. -type f-user root-exec chown Weber {} \; Change ownership under current directory to Weber
Note: {} is a special string, for each matching file, {} will be replaced with the corresponding filename;
Eg: Copy all found files to another directory:
Find. -type f-mtime +10-name "*.txt"-exec cp {} old \;
Combine multiple commands
Tips: If you need to execute multiple commands later, you can write multiple commands as a script. Then execute the script-exec the call;
-exec./commands.sh {} \;
the delimiter of the-print
Use ' \ n ' as the delimiter for the file by default;
-print0 uses ' I ' as a delimiter for the file, so that you can search for files that contain spaces; grep text Search
grep match_patten File//default access matching row
Common parameters
-O outputs only matching lines of text vs-v output only lines that do not match
-C Statistics The number of times the file contains text
Grep-c "text" filename
-N Print matching line numbers
-I ignores case when searching
-L print File name only
Recursive search of text in a multilevel directory (programmers search for code favorites):
grep "Class". -r-n
Match multiple patterns
Grep-e "Class"-E "vitural" file
The name of the grep output with the end character: (z)
grep "Test" file*-lz| xargs-0 RM
xargs command line parameter conversions
Xargs can convert input data into command-line arguments for specific commands, which can be combined with many commands. such as grep, such as find;
Convert multiline output to single-line output
Cat file.txt| Xargs
\ n is a delimiter between multiple lines of text
Convert a single line to multiline output
Cat Single.txt | Xargs-n 3
-N: Specifies the number of fields to display per line xargs parameter Description
-D definition delimiter (the default is a multiple-line delimiter for \ n)
-n Specifies that the output is multiple lines
-I {} Specifies a replacement string that is replaced when the Xargs is extended, when multiple parameters are required for a command to be executed
eg
Cat File.txt | Xargs-i {}./command.sh-p {}-1
-0: Specify the input delimiter
Eg: statistics of the number of program lines
Find Source_dir