Linux - Shell - find keywords in multiple files

https://www.cnblogs.com/xy14/p/11735343.html

1 Overview

  1. Find content in multiple files

2. think doing

  1. purpose
    1. In multiple files, find content
  2. ready
    1. Before looking through the contents in a single file
      1. tool
        1. awk
          1. premise
            1. Have a fixed format file
            2. Finding the time required fields
          2. example

            # print $0 就一句话, 所以 不要 {} 也可以
            > awk '{if($0 ~ <patter>){print $0}}' <fileName>
        2. grep
          1. premise
            1. This requirement is not so much
          2. example

            > cat <fileName> | grep <pattern> 
  3. example
    1. Claim
      1. Find multiple files, containing rows 1
      2. And know which documents in the
    2. file

      # 得到 1 2 3 三个文件, 每个文件的内容, 是 1 到 10 
      > seq 1 10 > 1
      > seq 1 10 > 2
      > seq 1 10 > 3
      # 可以用 循环写, 也不难对吧
      > for i in {1..3};do seq 1 10 > ${i};done

2. The idea of ​​1: cat | grep

  1. command

    > cat * | grep '1'
  2. result
    1. Export

      1
      10
      1
      10
      1
      10
    2. Not feeling
      1. 1 found
      2. But do not know which file content distribution

3. Ideas 2: find | xargs grep

  1. command

    # 不理解 find 的同学, 可以用 ls 代替
    # 我就不怎么理解
    # -name 支持 通配符, 我的目录下只有三个文件, 所以也可以不带
    > find . -type f -name "*" | xargs grep "1"
  2. result
    1. Export

      1:1
      1:10
      2:1
      2:10
      3:1
      3:10
    2. The OK
      1. With the file name, ha ha
        1. Get

4. Ideas

  1. Thinking
    1. find
      1. find
        1. To find a particular file
        2. The filename passed to grep
      2. grep
        1. Obtain multiple file names
        2. Find what files one by one
        3. Found, on the show
    2. cat
      1. cat
        1. The contents of all files into the flow
        2. Will spread to grep
      2. grep
        1. Receive a stream
          1. File name has been lost
        2. Find Content
          1. You can only find the content
  2. the difference
    1. grep can accept different things
      1. parameter
        1. After receiving the parameters, find files
      2. flow
        1. After receiving streams, which flow directly to the
        2. So the filename lost
    2. Question 1: Why the xargs with grep on the own line number, but with a cat would not?
      1. Grep behind with more than one file name, will bring their own
        1. When a single file stream or not with
Published 101 original articles · won praise 73 · views 120 000 +

Guess you like

Origin blog.csdn.net/usstmiracle/article/details/104628338