linux command summary query log

【background】

Troubleshoot problems online environment, and ultimately go online to check the log. And what command can be found quickly and accurately we need to find the log information, we also need to master a skill.

【command】

Linux command to view a variety of: tail, head, cat, tac, more

(A) tail command

tail [ -f ] [ -c Number | -n Number | -m Number | -b Number | -k Number ] [ File ]

Parameters:
. 1) -f read cycle
2) -q not display information
3) -v displays detailed processing information
4) -c Number Number reads the specified file from byte position
5) -n Number Number row is read from the position take the specified file
6) -m Number Number read from the multi-byte character position specified file, for example, assume that your file includes text, assuming that the -c parameter may result in truncated, but it will avoid this problem using the -m
7) -b Number 512-byte block location indicated by the Number reads the specified file.
8) -k Number reads the specified file from 1KB block location indicated Number.

In the above command, are related to Number, assumption is not specified, the default display 10 lines. Number may be used in front of the sign, which indicates the offset from the top is counted from the end.

Application:
Command Meaning
tail -f test.log view real-time log
tail -100f test.log view the last 100 lines of logging
log tail -n 10 test.log query logs the last 10 lines of the tail
tail -n +10 test.log 10 queries after all the log line
tail -fn 100 test.log cycle real-time look at the last 100 rows

(Ii) head command

Function with tail is the opposite, tail is the number of lines of the log review

Command Meaning
the first 10 lines of the log head -n 10 test.log query log file in the
head -n -10 test.log query log file logs all other lines except the last 10

(C) cat command

Function
1) once to display the entire file. filename CAT
2) create a file. CAT> filename
3) several files into one file. cat file1 file2> file

Parameters:
. 1) starts by one -n number of line numbers of all the outputs
is similar to 2) -b and -n, but no blank lines No.
3) -s when faced with more than two successive blank lines, on behalf of blank line is a line exchange
4) -c <number> is the number of bytes the display
5) -n <line number> shows the number of rows

Application
1) cat test.log | tail -n 1000 # 1000 final output file line test.log

2) cat -n test.log | grep "debug" # get key log line number

3) cat filename | tail -n +3000 | head -n 1000 # 3000 starting from the first row to display 1000 lines. Rows 3000 to 3999 is displayed

4) cat filename | head -n 3000 | tail -n +1000 # 1000 display lines to 3000 lines

5) cat -n textfile1> textfile2 # input textfile2 this file with a line number in the file content of textfile1

6) cat -b textfile1 textfile2 >> textfile3 # textfile1 the contents of the file and line number after textfile2 plus (without blank lines) will be appended to the contents in textfile3

7) cat error.log | grep -C 5 'nick' display file matching file foo string up and down the line and five lines
cat error.log | grep -B 5 'nick ' display foo and the first five rows
cat error.log | grep -A 5 'nick' foo display 5 and the rear row

(D) tac command

Function
tac is cat anti write over, it features contrast with the cat, cat is displayed continuously from the first row to the last row, while the reverse tac is shown by the last line to the first line.

(E) more command

Function
similar to the cat, but displayed page by page form. Basic Instructions Press the spacebar (space) to display the next page, press the return key (back) to the previous display, as well as character search function (similar to vi)

Parameter
1) the number of lines displayed a -num

2) -d prompt the user, the display [Press space to continue, 'q' to quit.] At the bottom of the screen, if the user pressed the wrong button, [Press 'h' for instructions.] Is displayed instead of 'beep' sound

3) -l ^ cancel meet the special characters will be suspended when the function L

4) -f row count the number of rows, the number of rows in fact, rather than after the wrap

5) -p does not show every page in a scrolling fashion, but to clear the screen before displaying content

6) -c -p with similar, except that the first display content and then clear the other old data

7) -s when faced with more than two consecutive blank lines, one line on the blank line substitution

8) -u without displaying marks (varies according to the TERM environment variable specified terminal)

9) + / pattern to search for the string (pattern) is displayed in front of each document, and then began to show after the string from

10) + num starts from the display line num

Application
Log If we find a lot of print on the screen is not convenient to view, use more and less commands,
such as: cat -n test.log | grep "Conditions" | more This will print the page, next page by clicking the spacebar

Command Meaning
more -s test.log log display page by page, if more than two consecutive blank lines blank line display line places
more +20 test.log beginning to show contents of the log from line 20

(F) grep command

Function:
The above commands are used in a few easy to find files, but when looking for documents, we often need to look through certain keywords, grep command can help us quickly find.

Parameters:
[Options] The main parameters:
-C: Only output count of matching lines.
-I: case-insensitive (only applicable to a single character).
-H: do not display the file name when querying multiple files.
-L: only the output file name contains characters match the query multiple files.
-N: display matching lines and line number.
-S: do not display an error message does not exist or no match text.
-V: Displays all lines matching text.
The main parameters of a regular expression pattern:
: Ignore original meaning of the regular expression special characters.
^: Match the regular expression of the start line.
$: The end of the line matches the regular expression.
<: Starting match the regular expression.

: Line to match the regular expression ends.
[]: A single character, such as [A] i.e. A compliance.
[-]: range, such as [AZ], i.e. A, B, C until Z meet the requirements.
. : All single character.

  • : There are characters in length can be zero.

Operation:
1, or operation
grep -E '123 | abc' filename // find the file (filename) contains 123 or abc comprising rows
egrep '123 | abc' filename // can likewise be implemented with egrep
awk '/ 123 | abc / 'filename // implementation of awk

2, the operating
grep pattern1 files | grep pattern2: show only matching rows of pattern1 and matching pattern2.

3. Other operating
grep -i pattern files: case-insensitive search. By default case-sensitive,
grep pattern the -l Files: lists only file names that match,
grep pattern Files -L: lists the file names do not match,
grep -w pattern Files: match only whole words, rather than a string of a portion (such as matching 'magic', not 'Magical'),
grep pattern Files -C Number: context matches are shown [number] lines,

Application
1) more joint.log | grep '60007746 ' # Returns number based on a query log

Many times, we all need to see the log down a few lines can be achieved by adding the relevant parameter.

2) more joint.log | grep -5 '60007746' 5 before and after the print line matching lines #

3) more joint.log | longitudinal grep -C 5 '60007746' # print matching rows 5 rows

4) more joint.log | grep -A 5 '60007746' # 5 after printing lines matching lines

5) more joint.log | grep -B 5 '60007746' # print the first 5 rows of matching lines

6) cat -n umltech-scan | grep 'reqBody' # in the log files to find a particular string: cat -n log file | grep 'Find what', if the content too much can add more through the back, through the viewing space at one

7) cat -n umltech-scan | save grep 'reqBody'> / test # condition will query the contents of the log to a file: cat -n log file | grep 'Find what'> Save location

(Vii) sed

Application
sed -n '5,10p' filename so you can view only the fifth line of the file to the line 10.

sed -n '/ 2018-02-06 15:05:38 /, / 2018-02-06 15: 20: 38 / p' umltech-scan
time period query log: sed -n '/ start /, / end time / p 'umltech-scan, the time format is "yyyy-mm-dd hh: mm: ss"

(Viii) vi

Application of
lookup file content keywords Method:
First Run>: vi filename
and enter>: / search string
a, or N (uppercase) lookup to find a press n

【to sum up】

Familiar with common Linux commands is that we must master a skill, so look for problems in the process can be more efficient speed.

Reference:
https://blog.csdn.net/hu_zhiting/article/details/88319528
https://jingyan.baidu.com/article/656db918fccd01e381249c2b.html
https://blog.csdn.net/lychbeyond/article/details/ 41,042,483
http://blog.sina.com.cn/s/blog_65e13da00100of1f.html
https://blog.csdn.net/u011641008/article/details/79527929

Guess you like

Origin www.cnblogs.com/chen-chen-chen/p/11641444.html