Linux view file content (8)

We know that you only need to double-click to open the file content on the graphical interface, so how to view the file content in the terminal window? Obviously, there needs to be a command that can display the contents of the file on the terminal interface.

There are two main commands to view the file content, namely cat and more, and a grep command to find the corresponding content of the file. Introduce slowly.

cat

The cat command can be used to view file contents, create files, merge files, and append file contents  . This article mainly introduces viewing file content. 

Its feature is that it will display all the content at once, which is suitable for viewing text files with less content.

Two text files can be prepared in the directory, one with more content and one with less content, namely demo1.txt and demo2.txt. Use the cat command to view separately:

cat demo1.txt

cat demo2.txt

To see the effect, I made the terminal window smaller, and a window that is too large can't see the difference:

 

It can be seen that no matter how long the content is, the cat command always displays all the content, and automatically goes to the next screen when the content exceeds the window . You may be wondering why I emphasize this point, as long as you can see the content. This is actually because another command more is different from it, which will be introduced below.

Before that, let's take a look at the two options of cat:

  • -b: Number non-empty output lines
  • -n: number all lines of output

In fact, it is to add a number in front of each line of output content, -b is to add a number to only non-empty lines, and -n is to add a number to all lines.

Speaking of which, I'm tragic again, my Linux environment is numbering all lines regardless of which option :

 

It is recommended that you try to install a virtual machine environment on your own computer, such as Ubuntu.

 

more

The difference of the more command is that when the file exceeds the window, the file will be displayed on a split screen. It is better to do it. Test the demo1.txt and demo2.txt files separately:

more demo1.txt

more demo2.txt

 

You can clearly see the difference. When the content of the file is small, it is no different from the cat effect. When the content of the file is large, a prompt message will be displayed, giving the percentage of the file that has been displayed so far: --More-- ( XX%), you can respond according to the following different actions:

Action keys Function
space bar Display the next screen of text
Enter Scroll text one line at a time
b scroll back one screen
f scroll forward one screen
q quit
/word Search word string (word can be replaced)

You can try it yourself.

 

grep

The grep command in Linux is a powerful text search tool that uses the format:

The target character filename that grep looks for

Now there is a test.txt file in the directory, look at its content:

cat test.txt

// result 
hello world  

this is test

hello linux

this is test

asdhsadyuscbjna

Search for hello in this file:

grep hello test.txt

// result 
hello world  
hello linux

As you can see, the search results will output the entire line containing the searched character. Note that if you want to search for multiple words, you should add quotation marks:

grep 'hello world' test.txt

 

Options

There are also many options for grep, only three are introduced here:

  • -n: Display matching lines and line numbers
  • -v: show all lines that do not contain matching text (equivalent to negation)
  • -i: ignore case

Add -n:

grep -n hello test.txt

// result 
2 :hello world  
 6 :hello linux

You can see that each line is preceded by the line number where they are located.

 

The -v command is equivalent to negating the lookup result:

grep -v hello test.txt

// result


this is test


this is test

asdhsadyuscbjnas

As you can see, it will output all lines that do not contain hello, and you can also add line numbers at this time:

grep -vn hello test.txt

//结果
1:
3:
4:this is test
5:
7:
8:this is test
9:
10:asdhsadyuscbjnas

 

-i ignores case and needless to say:

grep -i Hello test.txt

// result 
hello world  
hello linux

 

One more word, Linux command options can be used together, you can do this:

grep -nvi Hello test.txt

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325027766&siteId=291194637