Learn the grep and WC commands of the Linux command every day.

Source: Internet
Author: User

The grep command in a Linux system is a powerful text search tool that uses regular expressions to search for text and print matching lines. grep full name is global Regular Expr

Ession Print, which represents the global regular expression version, whose usage rights are all users.

Wc

1. Command format: WC [options] File ... 2. Command function: Statistics the number of bytes in the specified file, Word count, number of lines, and display the results of the output. This command counts the number of bytes, word count, and number of lines in the specified file. If the file name is not given, it is read from the standard input. The WC also gives the president count of the files specified. 3. Command parameters:-C count bytes. -L counts the number of rows. -M counts the number of characters. This flag cannot be used with the-C flag. -W count words. A word is defined as a string separated by a blank, a jump, or a newline character. -L Prints the length of the longest line. --HELP Display Help Information--version display version Information example: in file A, count the number of rows that Hello appears in:
grep Hello A | Wc-l

Count the number of Hello occurrences in file a:

Grep-o Hello A | Wc-l

The grep command and the WC command can be passed on the command line | Link to each other, as the input command originally had the same name as::::::: PIPE command!!!!

Yes, that's the feeling underneath.

  

  

1, Pipeline command only handle the correct output of the previous command, do not handle error output

2, the pipe command to the right, you must be able to receive the standard input stream command.

  

Cat test.sh | Grep-n ' good ' output: 1:echo very Good2:echo goodgrep is a text regular lookup command,-n means the output line number is found, and the input of grep is the output of the cat command, which is the standard output of the entire tesh.sh text
Cat test.sh | LSA.C               hah          sdk-gcc-1.0         notes 11~algorithm         huawei2016   sed_operation       notes 11a.out             LCSP         sed_operation~      public clion-2016.1.1    learngit     Shell               template clionprojects     mytest       Similargraphsearch  video core_uses_pid~    n1           software            pictures core_uses_piy~    n1~          SUBLIME-IMFIX.C     document core_uses_piz~    n2           test.sh             Untitled document ~examples.desktop          n2~ test.sh~            Download Game              Qt           the-first-project   music gnuplot-4.4.0     qtworkspace  UNIX                Desktop

Because LS does not support standard input, content in test.sh is discarded.

Cat test1.sh test.sh 2>/dev/null | Grep-n ' good ' 1:echo very good2:echo Good5:echo good# will test1.sh not find error output redirect output to/dev/null file, correct output sent to grep via pipeline

> is redirect

| is pipeline redirection

The difference is:

1, the left command should have standard output | The command on the right should accept the standard input
The command on the left should have standard output > the right can only be a file
The command on the left should require standard input < The right can only be a file

2, pipeline trigger two sub-Process Execution "|" On both sides of the program, and redirection is performed within a process

Nice to finally figure out why grep is always with | With other commands, < > is redirect,

Learn the grep and WC commands of the Linux command every day.

Related Article

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.