During development, you often need to find errors in online logs, such as looking at log4J logs. However, because logs are large, you need to use Linux to filter logs and locate errors. Common commands for viewing text include less, cat, more, tail, grep, and head
more
More is based on the VI editor, which loads the entire file at startup and displays the page one by one in separate screens, making it easy for operators to read the home page. More Supports keyword operations in the VI.
parameter
- -num Displays the number of rows
- +num from the num line is still displayed
- +/ PATTERN searches for the pattern before each file display, and then displays the pattern starting after the first two lines of the string
action
- Press the Space key to display the next screen of text
- Press Enter: only the next n lines of text are displayed. The default value of n is 1
- Press H to display the help screen
- Press B to display n lines. The default value of n is 1
- Press Q to exit the command
- Press = to output the line number of the current line
- Press: f: to output the file name and the line number of the current line
Command instances
Example 1: Display the contents of the file from line 3 onwards more +3 linux.log
Example 2: Find the first line in the file where the “day3” string appears and display the output more +/day3 linux.log starting from the first two lines of the output
$ more +/day3 linux.log … Skip 2018-04-DAY1 2018-04-day2 2018-04-day3
Example 3: list the files don’t limit the number of each occurrence when ll | more – 5
# the ls -l | more – 3 total dosage of 32 LRWXRWXRWX. 1 root root 7 September 20 09:47 bin – > usr/bin Dr – xr – xr – x. 4 root root on September 20, 4096 10:04 boot
At this point, if you want the next page to be 5, you can directly type 5 and press the space bar to output 5 lines.
less
The less command is similar to the more command in that it can be used to browse text files. The less command allows users to browse files forward or backward. The more command allows users to browse files forward only. Less does not load the entire file before viewing it. It is good for viewing large files. In general, less is more powerful.
action
- The Enter key moves down one line
- We moved up one row in y
- Space bar scroll down one screen
- B Scroll up one screen
- D Scroll down half screen
- H less
- U goes half a screen up
- W can specify which line to display first, starting from the next line after the specified number; For example, if 6 is specified, it will be displayed on line 7
- G jumps to the first line
- G jumps to the last line
- P n% jumps to n%, such as 10%, that is, 10% less than the contents of the entire file
- /pattern Searches for pattern. For example, /MAIL searches for MAIL words in files
- V Invokes the vi editor
- Q out less
- ! Command calls the SHELL and can run commands, such as! Ls Displays all files in the current directory in the current column
Commonly used parameters
- -c Refreshes the screen from top to bottom and displays the file content. Instead of scrolling through the bottom to refresh
- -f Forcibly opens the file. The binary file is displayed without warning
- -i Ignores case in the search. Unless the search string contains uppercase letters
- -i Ignores case unless the search string contains lowercase letters
- -m Displays the percentage of read files
- -m Indicates the percentage, line number, and total line number of the file read by the display method
- -n Displays the line number before each line
- -p pattern Searches for pattern. For example, to search for the word MAIL in /etc/profile, use less -p MAIL /etc/profile
- -s Displays consecutive blank lines as one blank line
- -q Mute the prompt tone on the terminal
- +num is displayed from the specified line
Common use
less -m -i -N linux.log
The screen displays line numbers, percentages when viewed, and is case insensitive when searched
history | less +10
History is displayed starting at line 10
cat
Cat is a tool for viewing and linking text, often paired with more. But the difference is that CAT can join and merge two files. For viewing, CAT is suitable for small text. So it’s not very good for looking at logs online. Common parameters are:
- -n or -number: indicates the number of all output lines starting with 1
- -b or –number-nonblank: similar to -n except that blank lines are not numbered
- -s or –squeeze-blank: When there are more than two consecutive blank lines, substitute for one blank line
- -a: displays non-printable characters and “$” at the end of the line
- -e: equivalent to the “-ve” option
- -t: equivalent to the “-vt” option
Command instances
cat m1
Displays the contents of file M1 on the screen
cat m1 m2
The m1 file is followed by the contents of M2
cat m1 m2 > file
Merge files M1 and m2 into file
head
The head command is used to display the beginning of a file. By default, the head command displays the first 10 lines of the file.
Commonly used parameters
- -n< number > : specifies the number of lines to display the header content
- -c< character number > : specifies the number of characters to display the header content
- -v: always displays the header information of the file name
- -q: does not display the header information of the file name
Common use
Head-n line numeric file name;
For example, if we display the first 4 lines of linux.log, we can:
head -n 4
grep
Grep (Global Search Regular expression(RE) and print out the line) is a powerful text search tool. It can use regular expressions to search text and print out matching lines.
Common use
grep -E ‘at |Execption | exception | Caused by’ log.log
Use the regular expression to search for the row in the log
grep match_pattern log.log
Search for a word in the log, and the command line returns a line of text containing the word
grep “match_pattern” file_1 file_2 file_3 …
Look in multiple files
grep “match_pattern” file_name –color=auto
Mark the matching color –color=auto
grep -C10 pattern filename
Query 10 lines before and after the keyword
Ongoing record