当前位置:网站首页>Introduction to shell programming (4) text processing

Introduction to shell programming (4) text processing

2020-11-16 18:58:51 Super_ Charles

1. File search command

  1. find command , Grammar format :find Search path [ Options ] Search content
Options analysis
-name Search by filename
-iname Search by filename , Do not distinguish file name size
-inum according to inode Search on
-type According to the type of document (f: file ,d: Catalog ,c: Character device file ,b: Block device file ,l: Link to the file ,p: Pipeline files ) Search for
-size Depending on the file size ( Company :ckMGTP) Search for ,- Less than ,+ Greater than , For example, find /etc The directory is larger than 1M The file of :find /etc -size +1M
-mtime According to the modification time ( Company :smhdw) Search for
-ctime According to the creation time ( Company :smhdw) Search for
-atime According to the time interval when being visited ( Company :smhdw) Search for
-mmin n Minutes to (-n: Inside ,+n: Outside ) Modified files in
-mindepth from n Level subdirectories start searching , At most n-1 Level subdirectories
-depth The search depth is n The file of , Under the specified directory n Layer files
-empty Retrieve an empty file or directory
-perm Search according to file permissions
-ls Print the details of the searched file
-delete Delete the retrieved file
-exec Common operations for search files ("-exec" and "-ok" be similar , Perform specific operations on files ,"-ok" After getting the confirmation order , Will execute ;-print Printout )
  1. locate command , differ find The command is to search the entire disk ,locate The command is to find... In the database file

    • find Is the default global match ,locate The default partial match
    • After the file is updated , use updatedb Command to update the file to the database ( By default, the system will automatically update to the database the next day ), otherwise locate Can't find
  2. whereis command , Can only be used for program name search

    • Command parameter : Binary ( Parameters -b)、man Documentation ( Parameters -m) And source code files ( Parameters -s). If the parameter is omitted , Then all information .
  3. which command , Find only binary files

2. Linux Three swordsmen in text processing

Three swordsman tools for text processing grep,sed and awk All based on line processing , They read the data line by line , Process one line before processing the next .

2.1 File processing three swordsmen grep

grep command , Finding qualified for file string .

  • Grammar format :
    • grammar 1:grep [option] [ pattern] [file1, file2..]
    • grammar 2:command | grep [option] [pattern]
Options (option) analysis
-v Don't show matching line information
-i Ignore case when searching
-n According to the line Numbers
-r Recursively searching
-E Support for extended regular expressions
-F Don't match by regular expression , Match the literal meaning of a string
-c Show only the total number of matching rows
-w Match the whole word
-x Matching entire line
-l Show only filename , Don't show content
-s Do not display error messages
  • grep and egrep
    • grep Extended regular expressions are not supported by default , Only basic regular expressions are supported
    • Use grep-E Can support extended regular expression use
    • egrep Can support extended regular expressions , And grep-E Equivalent

2.2 File processing three swordsmen sed

sed(Stream Editor), stream editor . Line by line processing of standard output or files

  • Grammar format :
    • grammar 1:stdout |sed [option] "pattern command"
    • grammar 2:sed [option] "pattern command" file
Options (option) analysis
-n Print only pattern matching lines
-e Go straight to the command line sed edit , The default option
-f Edit actions are saved in a file , Specify file execution
-r Support for extended regular expressions
-i Directly modify the contents of the document
Matching mode (pattern) analysis
10command Match to 10 That's ok
10,20command Match from 10 OK, let's start , To the first 20 End of line 10,
10,+5command Match from 10 OK, let's start , To the first 16 End of line
/pattern1/command Match to pattern1 The line of
/pattern1/,/pattern2/command Match to pattern1 Start of line for , To match to patern2 The end of the line
10,/pattern1/command Match from 10 OK, let's start , To match to pettern1 The end of the line
/pattern1/,10command Match to pattern1 Start of line for , To the first 10 End of line match
#  Print test.txt File first 17 That's ok 
sed -n "17p" test.txt

#  Print the document 10 To 20 That's ok 
sed -n "10,20p" test.txt

#  Print test.txt In the document, from page 10 OK, let's start , Add... To the back 5 That's ok 
sed -n "10,+5p" test.txt

#  Print test.txt In file root Beginning line 
sed -n "/^root/p" test.txt

#  Print test.txt The first one in the file matches with ftp Beginning line , To mail The beginning line ends 
sed -n "/^ftp/,/^mail/p" test.txt

#  Print test.txt In the document, from page 4 Line start matching , Until hdfs The beginning line ends 
sed -n "4,/^hdfs/p" test.txt

#  Print test.txt Match in file root The line of , Until the first 10 End of line 
sed -n "/root/,10p"test.txt

sed The edit command in the

Category Edit command meaning
Inquire about p Print
= Show only line numbers
increase a Append after line
i Add... Before the line
r External files read in , Append after line
w The matching line is written to the external file
Delete d Delete
modify s/old/new Will be the first in the line old Replace with new
s/old/new/g Will be all in the line old Replace with new
s/old/new/2g Move the line from 2 individual old From the beginning to the rest of old Replace with new
s/old/new/ig Will be all in the line old Replace with new, Ignore case

版权声明
本文为[Super_ Charles]所创,转载请带上原文链接,感谢

随机推荐