Common commands for text processing under Linux

Source: Internet
Author: User

As we all know, in Linux under the "All documents" principle, all the configuration files can be edited and stored in the form of text. Therefore, the ability to manipulate text files determines whether you can responds very well under Linux. Today, write down a few of the commonly used text processing commands under Linux.


1. text File View command: Cat, more, less, head, tail


1.1 Cat command: Connect and display when a text file has more than one, connect it and display it all in one

eg

Cat/etc/inittab

Cat/etc/fstab

Cat/etc/inittab/etc/fstab


There are three major features of cat:
1. Display the entire file at once. # cat filename
2. Create a file from the keyboard. # cat > FileName
Only new files can be created and existing files cannot be edited.
3. Merge several files into one file: # cat file1 file2 > file

       

Common parameters for the cat command:

Cat-n or--number numbering the number of rows for all outputs starting from 1

CAT-E Display the line terminator of a text file

The line terminator for Linux is $, and the line terminator for Windows is $+ carriage return,

Therefore, the text files edited on Linux are displayed in Windows and are only one line

Cat command, end with CTRL + C, run on bash;


1.2 File view of the screen display: More, less, head, tail


More commands: The more command, which functions like cat, theCat command is the entire contents of the file displayed on the screen from top to bottom. more will be a page-by-page display to facilitate users to read pages. the more command reads the file backwards from the front, so it loads the entire file at startup.


strictly speaking the more command can only achieve "page back", because to achieve "page forward", the current cursor must be not at the end of the file

Space bar space (one screen back)

B Key back (one screen ahead)

Carriage return (one line back)

/(search string)


Less command:the less tool is also a tool for paging through files or other output, which is the most powerful tool for Linux to view the contents of a file. The usage of less is more resilient than more.


at more times, we have no way to turn to the front, You can only look back, but if you use less, you can use [PageUp] [PageDown] and other key functions to turn to look at the file , more easily used to view the contents of a file! In addition, in less you can have more search function, not only to search down, but also to search upward. less does not load the entire file until it is viewed


Space bar space (one screen back)

B Key back (one screen ahead)

Carriage return (one line back)

/(search string)

PgUp (up one page)

PGDN (PAGE Down)

/(search string)

? (Search string)



Head command:head is used to display the beginning of the file to the standard output, and the default head command prints the first 10 lines of its corresponding file.

View the first n rows of a file, the first 10 rows by default

-N num: previous num line


Tail Command:the tail command writes the file to standard output starting at the specified point.


-N num: The num line after viewing the end of the file

-F: View the end of the file, do not exit, wait to display new content appended to this file

Use the-f option of the tail command to easily view the log files that are being changed

TAIL-F filename will display the most trailer in filename on the screen.

and constantly refreshed so that you see the latest file content.


Three great weapons: (Must Be)

SED (Text processing Class), awk (Text Processing Class), Grap (Text lookup Class)


2. Text Processing related commands: Cut (cut), join (merge), SED, awk

2.1 Cut Command: detailed content, can see my blog: Text Processing class command of the Cut command detailed

-D: Specifies the field delimiter, which is the default space

-F: Specifies the field to display.

-F 1

-F 1,3 (show 1th and 3rd attribute values, discrete notation)

-F 1-3 (shows the 1th to 3rd attribute values, continuous notation)


2.2 Text sort commands: Sort, uniq

Text sort:

Sort ( does not affect content in the original file, only affects display content, equivalent to view, default ascending, ASCII)

-N (sort by numeric size, not default ASCII code)

-R (in reverse order, can be used with-N)

-t (Field delimiter)

-K (which field is sorted by keyword)

-U (the same row after sorting, only once)

-F (ignores character case when sorting)

An explanation of the sort command: See the Sort command in the text Sort command under my blog--linux


Uniq (report and ignore duplicate rows), what is a duplicate row: content is consistent, adjacent to each other

-D: Show only duplicate rows

-D:

-C: Shows the number of rows in a file that repeats


The function of the Uniq command is to remove duplicate line output from the file. (Does not change the original document)

Uniq--help can see how the command parameters are used.

Uniq File1 Displays the contents of the file1, repeating rows are displayed only once.

Uniq-c File1 Displays the contents of the file1, repeating rows are displayed only once. List the number of times the bank appears in the file before each line.

uniq-d File1 displays only the rows that recur in the file1, and the repeating rows are displayed only once.

uniq-d File1 displays only the rows that appear repeatedly in the file1, and the repeating rows are all displayed.

Uniq-f 2 File1 ignores the first two words in each line, displays the contents of the File1, and repeats the rows only once.

Uniq-i File1 ignores the size, displays the contents of the File1, and repeats the rows only once.

Uniq-s 5 File1 ignores the first 5 letters of each line, displays the contents of the File1, and repeats the rows only once.

Uniq-u file1 only displays rows that are not duplicates in File1.

Uniq-w 5 File1 only compares the first 5 letters of each line, displays the contents of the File1, and repeats only once.


2.3 The WC command of the text Statistic command

Text statistics:

WC: (Statistics in one text: number of lines, number of words, number of characters)

Wc/etc/fstab

Wc-l/etc/fstab count rows

Wc-w/etc/fstab count words

Wc-c/etc/fstab Count bytes

Wc-m/etc/fstab count characters

Wc-l/etc/fstab shows how many characters the longest line contains


2.4-Character TR command for processing commands

Character Processing commands:

TR: converting or deleting characters

TR [OPTION] ... SET1 [SET2]

Set represents a character set

Example: TR AB AB

A comparison of one character

-D deletes all occurrences of characters in the character set and only needs to be given a character set


See my blog for details: The TR command of the character processing command under Linux




Common text processing commands under Linux are summarized as follows:

1. text File View command: Cat, more, less, head, tail

2. Text Processing related commands: Cut (cut), join (merge), SED, awk

3. Text Sort command: Sort, uniq

4. The WC command of the text statistics command

5. The TR command for character processing commands



Common commands for text processing under Linux

Related Article

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.