Analysis of the find/pg/grep/sed/awk command

Source: Internet
Author: User
Analysis of Java code using the find/pg/grep/sed/awk command & lt; spanstyle & quot; color: # 0000ff; & quot; & gt; & lt; strong & gt; [[find introduction] & lt;/strong & gt; & lt;/span & gt; findpathname-options [-print-exec-
Java code analysis of the find/pg/grep/sed/awk command [[Find introduction ]]]Find pathname-options [-print-exec-OK...]-print: The find command outputs the matching files to the standard output. Find the file whose permission is 7 5 5 in the current directory $ find. -perm 755-print: find the file name starting with an uppercase letter in the current directory and subdirectory $ find. -name "[A-Z] *" The file $ find/-mtime-5-print whose change time is within 5 days is to be located in the/var/adm Directory search for files whose modification time is earlier than 3: $ find/var/adm-mtime + 3-print all types of files except directories, available: $ find .! -Type d www.2cto.com [B-block device file; d-directory; c-character device file; p-pipeline file; l-symbol link file; f-common File: searches for files with a length greater than 4 kB in the current directory. you can use: $ find. -size + 4000c find command from the root directory of the file system, find a file named CON. FILE. It will first match all the files and then go to the subdirectory to find $ find/-name "CON. FILE "-depth after matching some files, you may want to perform some operations on them: find the files whose modification time was earlier than 5 days and delete them. find/-type f-mtime + 5-exec rm {}\; the exec option is followed by the command to be executed, then there is a pair of {}, a space and a \, and finally a semicolon to find all common files with 0 length in the system and list their complete paths, you can use: $ find/-type f-size 0-exec ls-l {}\; the last log file name contains numbers, so that we can see at a glance which file is the latest, which is the oldest one. The Admin. log file is compiled with the serial number admin. log.001.admin. log.002. The following find command will delete the admin. log file that was accessed in the/logs directory seven days ago and contains a digital suffix. This command only checks three digits, so the suffix of the corresponding log file should not exceed 999. $ Find/logs-name 'admin. log [0-9] [0-9] [0-9] e + 7-exec rm {}\; find and sort all the directories in the current file system. you can use: $ find. -type d-print-mount | sort can be used to find all the rmt tape devices in the system: $ find/dev/rmt [[Pg introduction] -- pagePg [option] the filename page displays the content of the specified file. When a page file is displayed, the last line will display the command waiting for the user to enter [option]: the command that can be entered has the following options. H this screen q or Q quit program Next page f skip a page forward d or ^ D next halfpage l next line $ last page/regex/search forward for regex? Regex? Or ^ regex ^ search backward for regex. or ^ L redraw screen w or z set page size and go to next page s filename save current file to filename! Command shell escape www.2cto.com p go to previous file n go to next file note: When the pg command is displayed at the end of the file, a file end information EOF (end of file) is displayed ). Example: % pg filename [[Grep introduction ]]]Grep [keyword] [options] filename [options] main parameter:-c: only counts matching rows are output. -I: it is case-insensitive (only applicable to single characters ). -H: when querying multiple files, the file name is not displayed. -L: when querying multiple files, only names containing matching characters are output. -N: displays matching rows and row numbers. -S: the error message that does not exist or does not match the text is not displayed. Blocking error! -V: displays all rows that do not contain matched text. Exclude! The main parameter of the regular expression pattern is \: ignore the original meaning of special characters in the regular expression. ^: Match the start line of the regular expression. $: Matches the end row of the regular expression. \ <: Starts from the row that matches the regular expression. \>: Ends with the row that matches the regular expression. []: A single character. for example, [A] indicates that A meets the requirements. [-]: Range, such as [A-Z], that is, A, B, C until Z all meet the requirements. .: All single characters. *: It can contain 0 characters. [Grep] and [sed] commands are used in the [awk] language in combination with {\} (metacharacters matching in case of characters. ^ Match only the beginning of the row --- $ ls-l | grep ^-$ match only the end of the row --- $ ls-l | grep k $ * only one single character followed *, match 0 or more. this single character [] only matches the characters in. It can be a single character or a character sequence. The range of character sequences in [] can be used. for example, [1-5] is used instead of [12345] \ to block the special meaning of a metacharacter. Because sometimes some metacharacters in s h e l have special meanings. \ Can make it meaningless. only match any single character: [5, 6 characters: XC :.... XC ....] pattern \ {n \} is only used to match the occurrence times of the previous pattern. N indicates the number of times pattern \ {n, \} m. The minimum number of times is n www.2cto.com pattern \ {n, m, but pattern appears in the class between n and m equivalent regular expression class equivalent regular expression [[: upper:] [A-Z] [[: alnum:] [0-9a-zA-Z] [[: lower:] [a-z] [[: space:] space or tab key [[: digit:] [0-9] [[: alpha:] [a-zA-Z: $ ls-l | grep '^ [^ d]' *************************** **************************************** ************* [[Sed introduction ]]]Sed (Stream EDitor) provides the EDitor sed [-nefri] command that automates editing on UNIX systems. input text 1. the format of sed command line is: sed [option] sed command input file 2. the format of the sed script file is: sed [option]-f sed script file input file 3. use the sed script file with the sed command interpreter in the first line. Its format is: sed script file [options] common options for the input file:-n: Quiet (silent) mode. In general sed usage, all information from STDIN is usually listed on the screen. However, if the-n parameter is added, only the row (or action) that has been specially processed by sed will be listed. -E: directly edit the sed action in the command column mode. $ sed-e '1, 5d '-e's/test/check/'example-f: write the sed action in a file, and-f filename can execute the sed action in filename; -r: sed supports the syntax of extended regular notation. (The default is the basic regular expression syntax)-I: directly modify the file content to be read, rather than output by the screen. Sed-E' edit command 1'-E' edit command 2'... file replacement Mark: g: indicates full replacement in the row. P: print rows. W: write the row into a file. W file x: Swap the text in the template block and the text in the buffer. Y: translates a character into another character (but not used in regular expressions) $ sed-e '/info/w tofile 'fromfile $ sed' 1, 3 w wfile 'rfile # write the file wfile. the screen displays the rfile content. common commands: (command) a -- append: Add. a can be followed by, these strings will appear in the new line (the current next line )~ C -- change: change. c can be followed by strings. these strings can replace rows between n1 and n2! D -- delete: delete, because it is delete, so d is usually followed by no comment; I -- insert: insert, and I can be followed by a string, these strings will appear in the new line (the previous line currently); p -- print: print, or print the selected data. Usually p will work with the sed-n parameter ~ S -- substuite: replacement, which can be replaced directly! Generally, this s action can be combined with regular notation! For example, 1, 20 s/old/new/g www.2cto.com $ sed '1, 3a drink tea 'file # add the string "drink tea" $ sed '1 "from the first row to the third row, 2c hi'file # replace the first line with the second line with the "Hi" $ sed '2, $ d' file # delete the second line to the last line $ sed-I '8, $ I bye 'file # insert a "bye" $ sed-n' 5 line between the eighth row and the last row in the file, 9p' file #5-9 lines of content are displayed on the screen (! No <-n> ~ 9 rows are repeatedly listed !) $ Sed-n'5, 9! P'file #'! 'Reverse $ sed-e's/test/& my car/'file # "test" is replaced with "test my car" $ sed-e's/(test) (my) (car) /[2 3 1]/'file # $ sed '/display/s/info/g' file1 file2 # Replace "info" in the row containing "display" in the file is "information ". # If there is no 'g', only the first 'info' of each row is replaced with "information ". (The separator that follows the s command is regarded as a new separator) $ sed-e '/^ $/d' file # Delete all empty rows $ sed-e' y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'file # replace lowercase letters with uppercase letters $ sed-n-e 'N'-e 'P' file # output an even row $ sed-n-e 'n'-e 'P' file # output an odd row $ sed-e'/date/ q'-E' 1, 3d 'file # when a row with 'date' is encountered, the content after the file is not processed $ sed-n' = 'File # print the travel number only $ sed-E' = 'File # row number + row content -------------------------------------------------- file: 12321 # FDGH #45.12 ^ M 00325 # GJSD #32.56 ^ M 02556 # GFGF #98.52 ^ M 1) replace all (#) with one space (##) symbol; 2) delete the first 0 (00) in the starting field; 3) delete the end of the row control character (^ M ); $ sed's/# // g' file $ sed's/^ 0 * // g' file $ sed's/\ ^ M // g' file $ sed -e's/# // g'-e's/^ 0 * // g'-e's/\ ^ M // g' file ----------------------------------------------------*** **************************************** ************************************* [[Awk introduction ]]]Awk [-f separator] 'commands' input-file (s) www.2cto.com awk' BEGIN {actions} {actions; actions ......}...... END {actions} 'inputfile ** Where BEGIN {actions} and END {actions} are optional !! BEGIN, END must be in uppercase !!!! ** If the mode is not specified, awk always matches or prints the number of rows. ** The actual action is indicated in braces {}. ** if the action is not specified, awk prints all browsing records ** the browsing domain is marked as $1, $2... $ n (separated by commas). All fields are: $0 ** print one or all fields. run the print command $ awk '{print $1, $2, $3} 'script.txt # The output result $ awk '{print $1, $2, $3}' script.txt> outfile.txt # is saved to the file, the output result $ awk '{print $0}' script | tee outfile.txt # is saved to the file, output result $ awk 'In in {print "aa \ tbb \ n ----------------"} {print $1 "\ t" $2} END {print "end of report "} 'Script.txt & AND: The statement must both be true. | OR: The statement matches both sides of the statement OR one of them to true.! Non: inverse ~ Match regular expression !~ Do not match the regular expression $ awk '$2 ~ /Politics/{print $0} 'script.txt $ awk' $2 ~ /Emy of Political Science and Law/'script.txt $ awk '$2 = "Emy of Political Science and Law" {print $0}' script.txt $ awk '{if ($2 ~ /Political Science and Law College /) print $0} 'script.txt [awk built-in variables] ARGC -- number of command line parameters ARGV -- command line parameter arrangement ENVIRON -- support for using FILENAME for system environment variables in the queue -- Name of the file browsed by awk www.2cto.com FNR -- Number of browsing file records FS -- set the input domain separator, equivalent to command line-F option NF -- Number of browsing records domain NR -- Number of read records OFS -- output domain separator ORS -- output record separator RS -- control record separator [awk built-in string function] gsub (r, s) replace r ---- $ awk 'gsub (/1002/, 1102) {print $0} 'file gsub (r, s, t) with s throughout $0) replace r ---- $ index (s, t) with s in the entire t to return the first position of string t in s ---- $ awk 'In in {print index (hello, el )} 'File length (s) returns the length of s ---- $ awk '$1 = "kk" {print length ($1) "" $1}' file match (s, r) test if s contains a string matching r ---- $ awk 'In in {print match ("hello",/e/)} 'file split (s, a, fs) divide s into series a ---- $ awk 'BEGIN {print split ("0461-3205-1656", array, "-")} 'file sprint (fmt, exp) on fs) returns the exp ---- $ sub (r, s) formatted by fmt) replace s ---- $ awk '$1 = "kk" substr ($, 5) 'file substr (s, p) with the leftmost longest substring in $0) returns the suffix starting with p in string s ---- $ awk 'In in {print match ("hello",/e/)} 'substr (s, p, n) returns the suffix of n starting from p in string s ---- $ [blocked sequence used in awk] \ B Return key \ f go to paper form \ n New Line \ r return key \ t tab key \ ddd octal value \ c any other special characters, for example, \ is the backslash symbol [awk printf modifier]-the left-aligned step of the www.2cto.com Width field, and 0 represents the 0 step. the maximum length of a prec string, or the number of digits on the right of the decimal point [awk printf format] % c ASCII character --- $ echo "65" | awk '{print "% c ", $0} '% d integer --- % e floating point number, scientific notation --- % f floating point number, for example (123.44) --- % g awk determines which floating point number is used to convert e or f --- % o octal number --- % s character --- % x hexadecimal number string --- author 185800961

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.