View cat, less, more, tail, Head, sed

Source: Internet
Author: User

This article describes several tools for viewing common file content, such as CAT, more, less, Head, and tail. We will introduce the most common parameters and actions of these tools to new users, it allows new users to get started in just a few minutes. This article is intended for the dark console.

 

1. CAT tool for displaying the file connection content;

Cat is a tool for viewing and connecting text files. View the content of a file. It is easy to use cat, that is, the file name is directly followed by CAT.

For example:
 

[root@localhost ~]# cat /etc/fstab

To make it easier for new students to master this tool flexibly, let's talk about common parameters;


1.0 cat syntax structure;

Cat [Option] [file]...
Option-A, -- show-all is equivalent to-vet-B, -- number-nonblank is equivalent to-ve-E, -- show-ends: $-N is displayed at the end of each line. -- number indicates the number of all rows to be output-s, -- Squeeze-blank does not output multiple empty rows-T is equivalent to-T and-VT, -- show-Tabs displays the hop character as ^ I-U (ignored)-V, -- show-nonprinting uses ^ and M-reference. Besides LFD and tab, -- help displays the help information and leaves.


1.1 cat view the file content instance;

[Root @ localhost ~] # Cat/etc/profile  Note: view the profile file in the/etc/directory;
[Root @ localhost ~] # Cat-B/etc/fstab Note: Check the profile content in the/etc/directory and number non-blank rows. The row number starts from 1;
[Root @ localhost ~] # Cat-N/etc/profile  Note: Number all rows (including blank rows) of the profile in the/etc directory are displayed;
[Root @ localhost ~] # Cat-E/etc/profile   Note: view the profile content under/etc/and add the $ symbol at the end of each line;

The cat parameter-N is similar to the NL tool. When the file content is output, the row number is added before each line;

[root@localhost ~]# cat -n /etc/profile
[root@localhost ~]# nl  /etc/profile

Cat can display the content of multiple files at the same time. For example, we can display the content of two files at the same time on a cat command;

[root@localhost ~]# cat /etc/fstab /etc/profile

For files with large content, cat can be transferred to the more tool through pipelines and viewed on a page by page;

[root@localhost ~]# cat /etc/fstab /etc/profile | more


1.2 create and connect cat file function instances;


Cat can create files. After creating a file, it must end with EOF or stop;

[Root @ localhost ~] # Cat>Linuxsir.org.txt<EOFNote: Create the linuxsir.org.txt file;
> I will test cat to create a file and input content for the file;     Note: This is the input content of linuxsir.org.txt;
> North-south-north testing;                 Note: This is the input content of linuxsir.org.txt;
> EOF Note: exit the editing status;

[Root @ localhost ~] # Cat linuxsir.org.txtNote: we need the contents of linuxsir.org.txt;

I will test cat to create a file and input content for the file;
North-south-north testing;

CAT also provides the ability to Append content to existing files;

[Root @ localhost ~] # CatLinuxsir.txt Note: parse the existing file linuxsir.txt content;
I am beinannanbei from linuxsir. org.  Note: Content line
I am writing a document for the cat command

[Root @ localhost ~] # Cat> linuxsir.txt <EOF Note: We append the content to the linuxsir.txt file;
> I will test the cat Append content to the document;     Note: This is the recovery content.
> OK?
> OK ~
> Presented to the North South
> EOF Note: exit with EOF;

[Root @ localhost ~] # Cat linuxsir.txtNote: Check the file content to see if the file is recovered successfully.
I am beinannanbei from linuxsir. org.
I am writing a document for the cat command

Let me test the cat Append content to the document; 
OK?
OK ~
Presented to the North South

Cat connects the content of multiple files and outputs them to a new file;

Suppose we have sir01.txt1_sir02.txand sir03.txt, And the content is as follows;

[root@localhost ~]# cat sir01.txt  
123456
i am testing

[root@localhost ~]# cat sir02.txt
56789
BeiNan Tested

[root@localhost ~]# cat sir03.txt
09876
linuxsir.org testing

I want to use Cat to connect the three files sir01.txt1_sir02.txtand sir03.txt together (I also say that the three files are connected together and exported to a new file sir04.txt.

Note:Running. The special value is that if you input a sir04.txt file that already exists, the sir04.txt content will be cleared.

[root@localhost ~]# cat sir01.txt sir02.txt sir03.txt > sir04.txt

[root@localhost ~]# more sir04.txt
123456
i am testing
56789
BeiNan Tested
09876
linuxsir.org testing

Cat appends the content of one or more existing files to an existing file.

[root@localhost ~]# cat sir00.txt
linuxsir.org forever

[root@localhost ~]# cat sir01.txt sir02.txt sir03.txt >> sir00.txt

[root@localhost ~]# cat sir00.txt
linuxsir.org forever
123456
i am testing
56789
BeiNan Tested
09876
linuxsir.org testing

Warning: You Need To Know> Create,> append. Never confuse. Making mistakes is not a joke;


2. More file content or output viewing tools;


More is one of our most commonly used tools. The most commonly used tool is to display the output content, and display it by PAGE based on the size of the window, and then display the file percentage;

[root@localhost ~]# more /etc/profile


2.1 more syntax, parameters, and commands;

More [Parameter options] [files]
The parameters are as follows:+ Num is displayed from row num;-num is defined as row num; +/pattern is displayed from the first two rows of pattern;-C is displayed from the top of the screen; -D: Press space to continue, 'q' to quit. (Press the empty key to continue, press the Q key to exit), disable the ring function;-l ignore Ctrl + L (page feed) characters;-P clears the window instead of scrolling to form a page feed for the file. It is similar to the-C parameter.-S displays multiple consecutive empty rows as one line.-U removes the underline in the file content.

The action to exit more is Q.


2.2 more parameter application example;

[Root @ localhost ~] # More-DC/etc/profile  Note: The prompt is displayed at the top of the terminal or console;
[Root @ localhost ~] # More + 4/etc/profile    Note: lines 4th of profile are displayed;
[Root @ localhost ~] # More-4/etc/profile    Note: four lines are displayed on each screen;
[Root @ localhost ~] # More +/mail/etc/profile   Note: the first two lines of the first mail word in profile are displayed;


2.3 more action commands;

When we view a file with large content, we need to use more action commands. For example, CTRL + f (or space key) is to display a screen down, and CTRL + B is to return to the previous screen; the enter key can scroll down to display n rows. To use this key, the default value is 1 row;

Let's just talk about a few common ones. Just try it;

Enter: Down to N rows. It must be defined. The default value is 1 line. CTRL + F: scroll down to a screen; empty key: scroll down to a screen; Ctrl + B: return to the previous screen; = output the row number of the current row;: f the output file name and the row number of the current row; V calls the VI editor ;! Command to call shell and run the command; q to exit more

When viewing a file, we want to call VI to edit it. Do not forget the V Action Command, which is convenient;


2.4 examples of using other commands through the combination of pipelines and more;

For example, if we list files in a directory, we should learn to use more for paging display because there is too much content. This must be combined with pipelines, such:

[root@localhost ~]# ls -l /etc  |more


3. Less tools for viewing file content;

The less tool is also a paging display tool for files or other outputs. It should be said that it is an orthodox Linux tool for viewing file content and has extremely powerful functions. You are a beginner and I suggest using less. Because there are too many less content, we will introduce the most commonly used content;


3.1 less syntax format;

Less [parameters]File

Common Parameters
 

-C. Refresh the screen from top to bottom and display the file content. Instead of refreshing through the bottom scroll;
-F force open the file. No warning is prompted when the binary file is displayed;
-I case insensitive during search, unless the search string contains uppercase letters;
-I case-insensitive when searching, unless the search string contains lower-case letters;
-M: displays the percentage of Files Read;
-The percentage, row number, and total number of lines read by M-mode;
-N: output the row number before each row;
-PSearch for pattern. For example, if you search for the Word Mail in/etc/profile, use less-P mail/etc/profile.
-S displays multiple consecutive blank rows as one blank row;
-Q does not ring the bell on the terminal;

For example, when we display the/etc/profile content, let it display the row number;

[root@localhost ~]# less -N    /etc/profile


3.2 less action commands;

After entering less, we have to learn a few actions to make it easier for us to view the file content. The most important command to remember is Q, which allows less to terminate viewing the file and exit;

Action
 

The enter key moves down a row;
Y: move one row up;
The Space key scrolls down a screen;
B. Scroll up the screen;
D. Scroll down the half screen;
H less help;
U moves to the ocean half screen;
W specifies the row to be displayed, which is displayed from the next row of the specified number. For example, if the row is 6, the row is displayed from the second row;
G jumps to the first line;
G to the last line;
P n % jumps to N %, for example, 10%, that is, it is displayed starting from 10% of the entire file content;
/Pattern: Search for pattern. For example,/mail indicates searching for mail words in files;
V call the VI editor;
Q: Exit less
! Command to call shell, you can run commands; for example! Ls displays all files in the current directory of the current column;

As for the less action, there is too much content. It is best to check man less when using it. Here we will not give an example;


4. The head tool displays the first few lines of the file content;

The head is the number of rows before the content of a file;

Easy to use;

Head-NRow ValueFile Name;

For example, the first 10 lines of/etc/profile should be:

[root@localhost ~]# head -n 10 /etc/profile


5. The tail tool displays the last few lines of the file content;

Tail is the number of rows before the content of a file;

Easy to use;

Tail -NRow ValueFile Name;

For example, the last five lines of/etc/profile should be:

[root@localhost ~]# tail  -n 5 /etc/profile

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.