grep, tail, WC, awk file processing commands under Linux

Source: Internet
Author: User
Tags log log

Grep

The grep command in a Linux system is a powerful text search tool that uses regular expressions to search for text and match lines to print.

Command syntax:

Usage:grep [-ABCDEFGHHIIJLLMNOOQRSSUVVWXZ] [-A num] [-b num] [-c[num]
[-E Pattern] [-F file] [--binary-files=value] [--color=when]
[--context[=num]] [--directories=action] [--label] [--line-buffered]
[--null] [Pattern] [File ...]

Command instance:

-C: Calculates the number of rows to match and displays the results;

?  ping www.cnblogs.com > Blog.log | tail-f blog.log PING www.cnblogs.com (42.121.252.58): Data bytes bytes from 42.121.252.58:icmp_seq=0 ttl=32 time=26.616 ms64 byt Es from 42.121.252.58:icmp_seq=1 ttl=32 time=26.738 ms64 bytes from 42.121.252.58:icmp_seq=2 ttl=32 time=26.482 ms64 byt Es from 42.121.252.58:icmp_seq=3 ttl=32 time=26.485 ms64 bytes from 42.121.252.58:icmp_seq=4 ttl=32 time=26.325 ms^ C?   grep-c 26.616 blog.log1

-C 2: Displays the matching line, and displays the two lines before and after, that is, 5 rows are displayed altogether;

?  grep-c 2 26.616 blog.logping www.cnblogs.com (42.121.252.58): Data bytes64 bytes from 42.121.252.58:icmp_seq=0 tt L=32 time=26.616 ms64 bytes from 42.121.252.58:icmp_seq=1 ttl=32 time=26.738 ms64 bytes from 42.121.252.58:icmp_seq=2 tt L=32 time=26.482 Ms

-A 2: Displays the matching line and displays the next two lines;

?  grep-a 2 26.616 blog.log64 bytes from 42.121.252.58:icmp_seq=0 ttl=32 time=26.616 ms64 bytes from 42.121.252.58:ICM P_seq=1 ttl=32 time=26.738 ms64 bytes from 42.121.252.58:icmp_seq=2 ttl=32 time=26.482 Ms

-V: Displays all rows that do not contain matching rows;

?  grep-v 26.616 blog.logping www.cnblogs.com (42.121.252.58): Data bytes64 bytes from 42.121.252.58:icmp_seq=1 ttl= time=26.738 ms64 bytes from 42.121.252.58:icmp_seq=2 ttl=32 time=26.482 ms64 bytes from 42.121.252.58:icmp_seq=3 ttl= time=26.485 ms64 bytes from 42.121.252.58:icmp_seq=4 ttl=32 time=26.325 Ms

-color: Display matching content and highlight with different colors;

?  grep--color 26.616 blog.log64 bytes from 42.121.252.58:icmp_seq=0 ttl=32 time=26.616 Ms
Tail

The tail command is the most commonly used command for the online machine to view log, which can be written to standard output from a specified point, and tail-f can view the log files that are being played out, allowing you to see the latest log log.

Command syntax:

Usage:tail [-F |-f |-r] [-Q] [-B # |-C # |-N #] [file ...]

Command instance:

-F: Monitor file growth;

?  ping www.cnblogs.com > Blog.log | tail-f blog.logping www.cnblogs.com (42.121.252.58): Data bytes64 bytes from 42.121.252.58:icmp_seq=0 ttl=32 time=26.250 ms64 bytes from 42.121.252.58:icmp_seq=1 ttl=32 time=25.807 ms64 bytes from 42.121.252.58:icmp_seq=2 ttl=32 time=25.966 ms64 bytes from 42.121.252.58:icmp_seq=3 ttl=32 time=25.939 ms64 bytes from 42.121.252.58:icmp_seq=4 ttl=32 time=25.833 ms64 bytes from 42.121.252.58:icmp_seq=5 ttl=32 time=25.862  MS has been shown Go down ... 

-Q: As opposed to-F, the file content is displayed directly, the default display of the file from the forward Number 10 lines of content;

?  tail-q blog.logping www.cnblogs.com (42.121.252.58): Data bytes64 bytes from 42.121.252.58:icmp_seq=0 ttl=32 time =26.250 ms64 bytes from 42.121.252.58:icmp_seq=1 ttl=32 time=25.807 ms64 bytes from 42.121.252.58:icmp_seq=2 ttl=32 time =25.966 ms64 bytes from 42.121.252.58:icmp_seq=3 ttl=32 time=25.939 ms64 bytes from 42.121.252.58:icmp_seq=4 ttl=32 time =25.833 ms64 bytes from 42.121.252.58:icmp_seq=5 ttl=32 time=25.862 Ms

-N: From the forward number, display the specified number of lines, general-F combined with:-FN, such as-fn 20, the last 20 lines of the current display file, and constantly display the latest content of the file;

?  ping www.cnblogs.com > Blog.log | tail-fn 1 blog.logping www.cnblogs.com (42.121.252.58): Data bytes64 bytes fr Om 42.121.252.58:icmp_seq=0 ttl=32 time=25.813 ms64 bytes from 42.121.252.58:icmp_seq=1 ttl=32 time=26.363 Ms64 bytes fr Om 42.121.252.58:icmp_seq=2 ttl=32 time=26.218 ms64 bytes from 42.121.252.58:icmp_seq=3 ttl=32 time=26.292  MS has been Show down ... 

Displays the line number (not the line number of the file, but the line number currently displayed) while displaying the contents of the file:

?  ping www.baidu.com > Baidu.log | tail-fn baidu.log | awk ' {print nr,$0} ' or ping www.baidu.com > Baidu.log | TAIL-FN Baidu.log |  Cat-n "1 PING www.a.shifen.com (61.135.169.125): Data bytes2 bytes from 61.135.169.125:icmp_seq=0 ttl=51 time=6.030 Ms3 bytes from 61.135.169.125:icmp_seq=1 ttl=51 time=3.815 ms4 * bytes from 61.135.169.125:icmp_seq=2 ttl=51 time=3 .964 ms5 bytes from 61.135.169.125:icmp_seq=3 ttl=51 time=3.775 Ms

From the forward number of files 2 lines directly displayed:

Tail 2 Baidu.log

Show the remainder of the file starting from the second line of the file:

Tail -n +2 baidu.log
Wc

The Linux WC command is used to calculate the number of words. With the WC instruction we can calculate the number of bytes, words, or columns of a file, without a filename or file name of "-", then the WC will read the data from the standard input device.

Command syntax:

USAGE:WC [-CLMW] [File ...]

Command instance:

-C or--bytes or--chars displays the number of bytes:

?  ~ Cat blog.logping www.cnblogs.com (42.121.252.58): Data bytes64 bytes from 42.121.252.58:icmp_seq=0 ttl=32 time=2 5.762 ms64 bytes from 42.121.252.58:icmp_seq=1 ttl=32 time=25.733 ms64 bytes from 42.121.252.58:icmp_seq=2 ttl=32 time=2 6.556 MS?  ~ wc-c blog.log     238 blog.log

Display line number:-L, display word count or Word number:-W:

?  ~ wc-l blog.log       4 blog.log?  ~ wc-w blog.log      blog.log

However, the above can be directly WC filename, the meaning of the output value corresponds to the above two examples:

?  WC Blog.log        4       -     238 Blog.log
Awk

Awk is a language for working with text files and is a powerful text analysis tool.

Command syntax:

awk [option parameter] ' script ' var=value file(s)

Or

< Span class= "KWD" >< Span class= "PLN" >awk [option parameter] -f scriptfile var= (s

Command instance:

< Span class= "pun" >< Span class= "pun" ><  Span class= "KWD" > Look directly at the chestnuts, it doesn't explain:

?  ~ Cat blog.logping www.cnblogs.com (42.121.252.58): Data bytes64 bytes from 42.121.252.58:icmp_seq=0 ttl=32 time=2 5.762 ms64 bytes from 42.121.252.58:icmp_seq=1 ttl=32 time=25.733 ms64 bytes from 42.121.252.58:icmp_seq=2 ttl=32 time=2 6.556 MS?  ~ awk ' {print $ blog.logPINGwww.cnblogs.com64bytes64bytes64bytes} '
?  ~ awk ' {print $ "->>" $ blog.logping->> www.cnblogs.com64->> bytes64->> bytes64->> bytes

For Awk's details, look here.

grep, tail, WC, awk file processing commands under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.