grep using a character vector with multiple patterns

前端 未结 10 2331
猫巷女王i
猫巷女王i 2020-11-22 04:29

I am trying to use grep to test whether a vector of strings are present in an another vector or not, and to output the values that are present (the matching pat

10条回答
  •  忘了有多久
    2020-11-22 05:31

    I suggest writing a little script and doing multiple searches with Grep. I've never found a way to search for multiple patterns, and believe me, I've looked!

    Like so, your shell file, with an embedded string:

     #!/bin/bash 
     grep *A6* "Alex A1 Alex A6 Alex A7 Bob A1 Chris A9 Chris A6";
     grep *A7* "Alex A1 Alex A6 Alex A7 Bob A1 Chris A9 Chris A6";
     grep *A8* "Alex A1 Alex A6 Alex A7 Bob A1 Chris A9 Chris A6";
    

    Then run by typing myshell.sh.

    If you want to be able to pass in the string on the command line, do it like this, with a shell argument--this is bash notation btw:

     #!/bin/bash 
     $stingtomatch = "${1}";
     grep *A6* "${stingtomatch}";
     grep *A7* "${stingtomatch}";
     grep *A8* "${stingtomatch}";
    

    And so forth.

    If there are a lot of patterns to match, you can put it in a for loop.

提交回复
热议问题