Advanced Linux Commands-sort, uniq, cut, sed, grep, find, awk

Source: Internet
Author: User

command difficulty in general, there are simple difficulties, parameters are often used in the work. If it involves some rare parameters also please Baidu or man a bit.

sort(Reference learning site: http://www.cnblogs.com/dong008259/archive/2011/12/08/2281214.html)

Function: Sort according to different data types

Format:sort [-cmnrtk][source file][-o output file]

Description

If there is a file "1.txt", if there is a list of numbers, respectively, 1,2,2,3,5,4

    - n: Sort by numeric size--when numbers are sorted by character, the 2:10 big cat 1.txt | Sort-n

    - u: Removing duplicate rows in the output row cat 1.txt | Sort-u

    - R: reverse order cat 1.txt | Sort-r

    - o: Output to source file (if using sort-n 1.txt > 1.txt, will result in loss of file content using Sort-n 1.txt-o 1.txt)

    - T(delimiter),-k(interval number): Determines the sorted column-->sort-nr-t ":"-K 3/ETC/PASSWD the third column of the file in the form of a number F reverse sort

Uniq (Reference Learning site: http://blog.csdn.net/e421083458/article/details/37913181)

Function: reads the input file and compares adjacent rows. To remove duplicate rows. (You need to sort first with sort before using)

Format: uniq [-CDU] File

Description

    - C: Add the number of times each line appears in the input file before the output line

    - D: Show only duplicate rows

    - u: Show only rows that are not duplicates

Example: CAT/ETC/PASSWD | Awk-f ":" ' {print $7} ' | Sort |uniq-c-View the types and quantities of interpreters in passwd

Cut(refer to Learning Address: http://www.cnblogs.com/dong008259/archive/2011/12/09/2282679.html)

Features: Content Selection of files, operation of all rows

Format: Cut [-BCDFN] File

Description

    - b : Split in bytes. "These byte locations will ignore multibyte character boundaries unless you also specify the-n flag" (number/letter/symbol, one byte, man for three bytes echo "Good" | Cut-b 4-6

    - C : Split in characters

    - D : Custom delimiter, can only be a single character. This distinction is more convenient than awk, sometimes awk.

-F : Used with-D to specify which area to display.

    - n : Cancels splitting multibyte characters. Used only with the-B flag. If the last byte of the character falls within the range of the <br/> indicated by the List parameter of the-B flag

Example:

cat/etc/passwd | Cut-d ":"-f7 |sort | Uniq (see how many user interpreter types are in/etc/passwd)

sed(Learning reference address: http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856901.html, http://www.iteye.com/topic/587673)

Function: Online editor, automatically edit one or more files, simplify the repeated operation of the file, write the conversion program and so on. does not change the original file content

Processing a row of data, first putting the data into a temporary buffer, the SED command processes the contents of the buffer, and then outputs it to the screen . Next line, know the end

Address: Specify the edit corresponding line by address (number), separated by commas two digits as the starting position (including), Example: 1,5 (first line to line fifth), $ denotes last row

Format: sed [-NEFR] [action]

Description

- N: Quiet (silent) mode, with the-n parameter, only the line (or action) that is specially processed by SED is listed - e: Multi-point Edit/command to perform SED action editing directly in command-line mode - I.: Directly modifies the contents of the read file, not the output to the terminal ( Modify the original file- F: Writes the action of SED directly in a file, and-f filename can run the SED action in filename(not commonly used) - R: The action of SED supports the syntax of extended formal notation. (The default is the basic formal French notation)(not commonly used) Action Section[N1,N2] Action N1, N2 is the action address, $ is the end of the line A:New, a can be followed by a string, and these strings will appear in a new line ( the next line of the current) ~ C:Replace, C can be followed by strings, these strings can replace the line between N1,N2! D:Delete, because it is deleted, so d is not usually followed by the content I:Insert, I can be followed by a string, and these strings will appear on a new line (the current line) P:Print, that is, to print a selected data. Normally p will run with parameter sed-n S:Instead, the work can be replaced directly, usually this s action can be paired with regular expressions! Example 1,20s/old/new/g example: New: sed ' 2aHello World '/etc/passwd: In the second row of added laterone line; Sed ' 2,3aHHH '/etc/passwd (will be added HHH in the third and fourth lines) Supersedes: sed ' 2, $cHHH '/etc/passwd (second line to last row superseded) Delete: sed ' 2,$ D'/ETC/PASSWD: Delete the second line to the last line insert: sed ' 2i hhh '/etc/passwd: The second line inserts a string, the original second line becomes the third row sed ' 2,5i hhh ' /ETC/PASSWD: Insert string in the middle of the second line to the fifth line print: Sed-n ' 2,3p '/etc/passwd: Adds the 2nd, 3 rows of content output: sed is a command of commonly used regular expressions and does not dig further deep. can refer to Baidu grep(Reference Learning site: http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856896.html) Features: General Regular Expression Parser, Abbreviations for general-purpose regular expressionsYes,AA text Search tool that uses regular expressions to search for text and prints matching lines. On the basis of grep, we have expanded egrep/fgrep and other information.format:grep[-ACINV] [--color=auto]'Search String'filename Description : - a : the binary file is searched for data in the form of a text file - C: Calculated to find'Search String'the number of - I : ignoring case differences, so case is considered the same - n : Output line number by the way - v: Reverse selection, which shows no'Search String'The content of that line! --color=auto: You can add a color display to the keywords you find. -A(n): After, the results of the lookup are connected to the industry (n) display - b(n): Before, action reference - C(n): Center, action parameter Regular Expressions[]: Match any one character-->grep-n "[1-9][3-9]" match start with 1-9 arbitrary data, followed by 3-9 any number, then the data range between 13~99 [^]: Reverse Selection ^, $: Start, end character,-->grep-n "^root.*root$" matches the line that starts with root and ends with root.: At least one character? : Any one character, 0 or 1 (requires the use of grep - e) *: Repeats the previous character (string) n (n>=0) +: Repeats the previous character more than 1 times (requires the use of grep - e)Example: Grep-a1-n--color=auto "^hd.*/bash$"/etc/passwd >> start with HD,/bash end, output line number, corresponding row and next row and highlight. Find(Reference Study Address: http://www.cnblogs.com/wanqieddy/archive/2011/06/09/2076785.html )Features: Search for files in the directory structure and perform the specified actions, involving more searches, and putting them in the background (add:& at the end of the command)format: Find pathname [options]Description: -name: Find files by file name -type: Search by file type, F: normal file D: Directory L: Symbolic link File b/c: block/character device file P: Pipe file -print: Output matched file to standard output (-print0: No Line break) -size: Match file Size (k/m/g) -perm: (permission) matching file permissions for example: Find ~-perm 755-type d-name "demon*" (The home directory lookup permission is 755 to start with the demon directory) -user/-group: Matching file belongs to main/group master for file query. Example: Find ~-user sunhuaj-name "hehe" (Find files in home directory subordinate to Sunhuaj named hehe) file metadata: Accesstime, Modifytime, Changetime +: Before the time; -: Within time (in the most recent time) -ctime: +/-n based on the creation time of the file "in the day dimension, span large;-cmin, Minute Dimension" -atiime: +/-based on the access time of the file "IBID.;-amin, Minute Dimension" -mtime: +/-based on the modified time of the file "IBID.;-mmin minute Dimension" -exec cmd {} \;: The Find command executes the shell command given by the parameter to the matching file, noting "{}" and "\;"     There are spaces example: 1, find files created within the day of 1 hours before the modified file of more than 3M find ~-ctime-1-mmin 60-size +3m-print 2, find empty files/directories Find/-empty 3. View the details of the target file find ~-name "*shj*"-exec ls-l {} \; awk(Refer to study site: http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858470.html)Features: Powerful text analysis tools, compared to grep lookup, sed editing, awk is especially powerful when it comes to analyzing data and generating reports. To put it simply, awk reads the file line-by-row, using a space as the default delimiter to slice each row, and then perform various analytical processes on the cut.format: awk ' {pattern + action} ' {filenames}     pattern: represents what AWK looks for in the data, in the case of a regular expression to be represented, surrounded by slashes       Action: A sequence of commands that are executed when a match is found.       Description: Curly braces ({}) do not need to always appear in the program, but they are used to group a series of instructions according to a specific pattern:     a, last-n 5 | awk ' {print $} ': Shows the most recently logged-in five accounts      awk Workflow: Reads a record with a ' \ n ' line break, then divides the record by the specified field delimiter, fills the field, and $ $ represents all fields, $ 1 represents the first field, $n represents the nth field. The default Domain delimiter is the "blank key" or "[tab] key", so the login user, $ $ $ means login User IP, and so on       b, CAT/ETC/PASSWD | Awk-f ":" ' {print $} ': Show only/etc/passwd users           CAT/ETC/PASSWD | Awk-f ":" ' {print $ \ t ' $7} ': Shows/etc/passwd's account and the shell corresponding to the account, while the account and Shell tab-Split       c, cat/etc/ passwd |awk-f ': ' BEGIN {print ' Name,shell '} {print $ ', ' $7} END {print ' Blue,/bin/nosh '} '         &N The Bsp; awk workflow is done by first executing the beging, then reading the file, reading a record with the/n line break, and then dividing the record by the specified field delimiter, populating the field, and $ $ for all fields, representing the first field, $n representing the nth field, The action action corresponding to the execution pattern is then started. Then start reading the second record ... Until all of the records have been read, end operation is finished       d, cat  /etc/passwd | awk '/root/': Search for aKeywords rootde line       e, awk-f: '/root/{print $7} '/etc/passwd: Search all rows with the root keyword, and display the corresponding shell   Reprint Please specify the source! Welcome Email communication: [Email protected]

Advanced Linux Commands-sort, uniq, cut, sed, grep, find, awk

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.