Extract pattern from a string

前端 未结 5 2154
耶瑟儿~
耶瑟儿~ 2020-12-16 02:21

In bash script, what is the easy way to extract a text pattern from a string?

For example, I want to extract X followed by 2 digits in the end of the st

5条回答
  •  佛祖请我去吃肉
    2020-12-16 02:41

    Lets take your input as

    Input.txt

    ASD123
    GHG11D3456
    FFSD11dfGH
    FF87SD54HJ
    

    And the pattern I want to find is "SD[digit][digit]"

    Code

    grep -o 'SD[0-9][0-9]' Input.txt

    Output

    SD12
    SD11
    SD54
    

    And if you want to use this in script...then you can assign the above code in a variable/array... that's according to your need.

提交回复
热议问题