Parsing only first regex match in a line with several matches

前端 未结 2 422
野性不改
野性不改 2021-01-21 15:42

Is it possible to have a regex that parses only a1bcdea1 from this line a1bcdea1ABCa1DEFa1 ?

This grep command does not work:

         


        
2条回答
  •  一生所求
    2021-01-21 16:14

    Here is a gnu awk solution using split function:

    awk '(n = split($0, a, /[a-zA-Z]1/, b)) > 1 {print b[1] a[2] b[2]}' file
    

    a1bcdea1
    

    This awk command splits each line on regex /[a-zA-Z]1/ and stores split tokens in array a and delimiters in array b.

提交回复
热议问题