Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the Linux text view commands that you must know?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Xiaobian to share with you what must not know Linux text view commands, I believe most people do not know how, so share this article for your reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!

Full text display--cat

cat may be a commonly used text view command, and the method of use is also very simple:

cat file #full text displayed in terminal cat -n file #display full text and display line number

Cat can also be used as a merge file:

cat file1 file2 >file3

This command merges the contents of file1 file2 into file3.

Display full text in reverse order--tac

tac is the reverse of cat; tac displays full text content in reverse order in units of behavior.

tac file

Pagination Display Text--more

cat outputs the entire text content to the terminal. Then it also brings a problem, if the text content is more, the previous content will be very inconvenient to view. The more command can be displayed in separate pages.

1. display content

more file

After that, you can use the keys to view the text. Common keys are as follows:

enter #Down n lines, default is 1 line blank #Scroll down one screen b #Scroll up one screen = #Output current line number:f #Output current file name and current line number q #quit

2. Display from specified row

more +10 file

This command displays the contents of file starting at line 10.

3. Display starts with matching string lines

more +/string file

This command starts the contents of file with the first two lines of string.

Browse anywhere Search Text--less

The basic functionality of the less command is not much different from that of the more command, but the less command can browse forward files, while the more command can only browse backward files, and less has more search capabilities.

Common usage:

less file #Browse file less -N file #Browse file and display line numbers for each line less -m file #Browse file and display percentages

Common keys are as follows:

f #Scroll forward one screen b #Scroll back one screen Enter or j #Move forward one line k #Move back one line G #Move to the last line g #Move to the first line/string #Search string down, nView next, NView previous result? string #Search string up, n View next, N View previous q #quit

Compared to the more command, the less command can search for strings that match what is needed.

In addition, less can also switch browsing between multiple files:

less file1 file2 file3 :n #Switch to next file:p #Switch to previous file:x #Switch to the first file:d #Remove files from current list

Display Text Head Content--head

The head command does what its name suggests, displaying the text at the beginning of a file.

Common usage is as follows:

head -n 100 file #Displays the first 100 lines of the file head -n -100 file #Displays the contents of the file except for the last 100 lines.

Show tail of text--tail

Similar to the head command, except that the tail command reads the end of text:

tail -100 file #Display the last 100 lines of file tail -n +100 file #Display the contents of file from line 100

Tail also has a more practical use for real-time text updates. For example, if there is a log file being written and updated in real time, you can use the command:

tail -f logFile

The updated log content will be printed to the terminal in real time, and the real-time log can be viewed.

Specify order to display text--sort

sort can be used to sort and display text, default is dictionary ascending.

For example, there is a text test.txt that reads as follows:

vim count fail help help dead apple

1. Display text in ascending order

Use the command:

sort test.txt apple count dead fail help help vim

Text content will be displayed in ascending order.

2. by decreasing order

Related parameters-r:

sort -r test.txt vim help help fail dead count apple

3. Remove duplicate rows

We can observe that the previous help has two lines, what if we don't want to see duplicate lines? You can use the parameter-u, for example:

sort -u test.txt apple count dead fail help vim

You can see that the help line is no longer repeated.

4. Sort by number

If we sort by dictionary, 10 will come before 2, so we need to sort by number size:

sort -n file

Due to the limited space in this article, I will not introduce it in this article. Later, I will introduce the magic of sort command separately.

Filter display text--sed

SED is a powerful stream editor, but this article only covers text viewing.

1. Display matching keyword rows

Sometimes to view logs, you may only need to view log lines that contain certain keywords:

sed -n "/string/p" logFile

The command above means to print a line containing string.

2. Print specified lines

sed -n "1,5p" logFile #print lines 1 to 5 sed -n '3,5{=;p}' logFile #print lines 3 to 5 and print line number sed -n "10p" logFIle #print line 10

More: Common Linux command introduction-sed

unhide text--uniq

Common usage is as follows:

uniq file #remove duplicate lines uniq -c file #remove duplicate lines and display the number of duplicates uniq -d file #display only duplicate lines uniq -u file #display only lines that occur once uniq -i file #ignore case and remove duplicate lines uniqe -w 10 file #consider the first 10 characters to be the same, i.e. duplicate

Text Edit View--vi

Viewing documents is also easy:

vi file

Vim, which evolved from vi and is known as the god of editors, has more powerful functions, which will not be expanded here.

The above is "must not know Linux text view command what" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report