Linux entry record: 17, Linux text/file processing commands

Source: Internet
Author: User
Tags diff

First, file browsing
Cat      View File contents More     page to view the contents of the file (only page down) less     view the contents of the file as a page turn (can page up and down) Head     view the first few lines of the file (default 10 lines) tail     View the tail lines of a file (default 10 lines)

  

Second, the regular matching print line

Command grep is used to globally match the regular expression and print the row:

grep ' MINGC '/etc/passwd        matches MINGC user information in the file and prints the line Find/-user MINGC | grep ". *\.png$" to  find all of the MINGC PNG files (pipeline operations)

Common parameters:

       -I ignores case-n displays the number of rows       where the result       does not match the regular line (similar to a take-back operation)-an the      specified n-line after the row of the result is included in the output-bn      The specified n row before the row of the result is included in the output

  

Three, text cut print field

Command cut is commonly used to cut lines of text and print certain fields:

CUT-D:FL/ETC/PASSWD    Print the 1th field (user name) of a colon split in a passwd file (user name listed in multiple rows) grep mingc/etc/passwd | cut-d:-f3   Print MINGC User Information summary colon-divided by the 3rd field (UID)

Common parameters:

-D       Specifies that the split character (default tab)       -F displays a field of a specific ordinal (starting from 1)-C       displays a specific range of characters (from the first to the first few)

Example:

grep mingc/etc/passwd | cut-d:-f3     Print MINGC user information in colon-delimited 3rd field (UID) grep mingc/etc/passwd | cut-d:-f6,7   print MINGC user home directory and login Shellgrep MINGC/ etc/passwd | Cut-c1-5       Print MINGC user information in 1th to 5th character grep mingc/etc/passwd | cut-c1-        Print MINGC user information All characters after 1th character grep mingc/etc/ passwd | Cut-c-5        print MINGC all characters before the 5th character in the user information

  

Iv. Text Statistics

Command WC is used to count the number of lines, words, and characters of a file:

WC test.md

The default output is one row without parameters, and the field format is:

Number of lines word number character file name

Common parameters:

-L counts only the number of rows----only the count of       words-c only counts bytes-       m only counts       characters

  

Five, text sorting

The command sort is used to sort the contents of the file (you can also sort stdin):

Sort filename

Common parameters:

        -R Reverse (reverse order) sort        -n based on numeric sort-F        ignoring case-u        de-repeating (excluding duplicate rows)-t< separator > Specify delimiter (General mate-K parameter used, simple segmentation meaningless)-K n      When specifying a separator, sort by nth field (ordinal n starts at 1)

The-R,-N,-t,-K parameters can be used together:

, each line of the contents of the Test.md file is separated by a colon, and the 3rd field is sorted in reverse order based on the number.

Vi. Elimination of duplicate rows

The command sort -u can exclude duplicate rows of file contents, but the side effects are sorted.

Command Uniq can reject the file contents (adjacent) Duplicate rows:

Vii. Comparison of texts

Command diff is used to compare the differences between two files:

Diff test1.md Test2.md

Common parameters:

       -I ignores case       -B ignores space character       -U unified Display comparison information (typically used to generate patch files)

Example:

Diff-u old.md new.md > Update.patch   generate update information for files to patch files

Eight, spell check

The command Aspell is used to display check spelling in English:

Aspell Check Filenameaspell List < filename

(CentOS 6.9 64-bit system does not seem to have this command, nor common, not detailed)

Nine, character conversion

The command TR is used to convert characters from standard input to processing. Redirects are required if the input from the file is processed.

Delete keyword:

tr-d ' keyword ' < filename

Convert case:

Tr ' A-Z ' A-Z ' < filename

Ten, stream editing--search and replace

The sed command is a streaming editing tool that can be used in conjunction with regular expressions. When processing a file stream, the currently processed row is stored in a temporary buffer, called the pattern space, and then the contents of the buffer are processed, and the contents of the buffer are sent to the screen after processing is complete. Then the next line is processed, so it repeats until the end of the file. The contents of the file do not change unless you use redirection to store the output. sed commands are primarily used to automatically edit one or more files, to simplify repetitive operations on files, to write conversion programs, and so on.

Grammar:

sed [options] ' command ' file (s) sed [options]-F scriptfile file (s)

Common parameters Options:

-e<script>   Specifies a script to handle the text-f<script>   Specify a script to process the text
-n Show only processed results

Common Command commands:

D    Delete Row D    Delete first line s    replace specified character h    copy content to buffer h    append content to buffer g    get buffer contents, replace current text g    get buffer contents, append to current text after P    Print line p    Print first line q    exit sed

Example:

Sed ' s/linux/unix/g ' filename      s means search substitution, Linux Replace with unix,g for global substitution, filename target file sed ' l,50s/linux/unix/g ' filename  1 to 50 rows for global substitution sed-e ' s/linux/unix/g '-e ' s/ming/mingc/g ' filename-  e parameter replaces multiple sed-f script filename             specifying a script to process the file

RELATED links: sed command-Linux commands Daquan

Linux entry record: 17, Linux text/file processing commands

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.