In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to view text in Linux, Xiaobian feels quite practical, so share it with you as a reference, I hope you can gain something after reading this article.
preface
In daily development, we often need to view various texts and logs on the server. This article mainly summarizes and summarizes the commonly used text and log viewing techniques, so that everyone can collect them for subsequent viewing:
tail command View log information
Real-time monitoring logs:
tail -f filename
Real-time monitoring of 10 lines of log information:
tail -10f filename
Look at the last 100 lines of log information at the end of the log:
tail -n 100 filename
View log information after 100 lines:
tail -n +100 filename
head command View text messages
Look at the first 100 lines of text:
head -n 100 filename
View the last 100 lines or more of text
head -n -100 filename
cat command View text messages
View all text content
cat filename
View the middle of the text between certain line ranges, say viewing text between lines 100-120:
cat -n filename |tail -n +100|head -n 20
However, individuals do not like to use this command to locate the text view, because it is too troublesome to view, the article will have a more simple command operation.
tac View text messages
The cat command is a bit of the opposite of the cat command. The cat command is more about presenting the text from the head to the end, while the tac command is about presenting the log content from the end of the text to the head.
tac filename
For example, let's say we use cat and tac to view the same piece of content simultaneously:
Display the text through different commands and you will find that the order of the text content is reversed.
Search for text content
Grep is a powerful text search tool that searches text using regular expressions and prints out matching lines.
Common ways to use grep:
Find in multiple files:
grep "match_pattern" file_1 file_2 file_3 ...
Tag Match Color--color=auto Options:
grep "match_pattern" file_name --color=auto //[The color item here can be selected according to the document description: always, never, auto]
Output all lines except-v option:
grep -v "match_pattern" file_name
Use the regular expression-E option:
grep -E "[1-9]+"
Output only matched content options:
grep -o -E "[a-z]+. " line
Count the number of rows in a file or text containing matching strings-c option:
grep -c "text" file_name
Output the number of rows containing matching strings-n option:
grep "text" -n file_name
After understanding the function points that grep command can accomplish, we can use it flexibly in practical work.
Sometimes we also encounter some need to view log information for a certain period of time. At this time, you can use grep to achieve this function. For example, if you want to view log information within 2019-08-06 22:00, you can enter the following command:
grep '2019-08-06 22' filename
This allows you to quickly navigate to the information you want to see.
Since the grep command can receive standard input data, we can usually use the pipe command character "|"to perform lookup operations in some standard output in progress.
For example, first read the contents of the file, and then use the help of the pipeline to forward the contents to grep for content filtering, such as the following command:
cat log.file |grep -n '2019-08-06 22:43'
Instead of using grep, you can also use sed to achieve the desired effect.
SED is a non-interactive editor that processes files (or inputs) line by line and sends the results to the screen.
Perhaps for beginners, it is more direct to talk about concepts than to come directly to a few practical cases.
Common ways of sed command:
Print only the first line of the file
sed -n '1p' filename
View the file from line 1 to line 10
sed -n '1,10p' filename
Delete the first line of text information
sed '1d' filename
Replace certain strings in text
sed 's/what you want to replace/what is replaced/g' e.g. sed 's/1/one /g' filename Replace 1 with one
After understanding some of the basic uses of sed, we can use it many times in practical work scenarios to strengthen our understanding of sed.
For example, specify the date range by sed command, for example, view the log records between 2019-08-06 22:43-22:44:
sed -n '/2019-08-06 22:43/,/2019-08-06 22:44/p' filename
The above mentioned the use of head and tail commands together to achieve a view of a section of the log file, but such an operation is a bit troublesome, you may wish to try using the sed command to operate.
For example, look at lines 1-20 of the log:
nl log.file | sed -n '1,10p'
Use the more command to turn pages
If the log file you want to view is too large, you can use the more command to page search, for example, set each page to display 10 pieces of data information:
more -10 filename
By using the more command, you can view the data displayed on each page, and by hitting the space bar, you can jump to the next page. The window also displays the basic progress of the text currently being read.
Having said that, let's do some simulation scenarios:
View log entries for the last occurrence of keyword 'test'
grep 'test' -A 10 log.file | tail -n 11
Here you need to understand the meaning of several parameters of the grep command:
grep 'name' -A 10 Display matches and the next 10 lines
grep 'name' -B 10 Display matches and the first 10 lines
grep 'name' -C 10 Display matches and the first 10 lines
The tail -n 11 command displays the currently displayed 10 lines and the matching line
Simply count the number of rows in a log where the keyword 'test' appears
Corresponding orders:
grep 'test' ./ log.file |wc -l
Here we can first output the contents of the text to the standard output, and then pass the data information to the wc command for statistics through the pipeline.
Some common parameters of wc command
-l Number of rows matched
-w matching words
-m Number of characters matched
Linux inside for text information viewing skills is too much, far from being limited to what I mentioned in this article, so in the actual work we can also help ourselves to improve the efficiency of the skills summarized and summarized.
About "how to view text in Linux" this article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.