How to extract a value from a string using regex and a shell?

前端 未结 7 970
Happy的楠姐
Happy的楠姐 2020-12-13 06:11

I am in shell and I have this string: 12 BBQ ,45 rofl, 89 lol

Using the regexp: \\d+ (?=rofl), I want 45 as a result.

7条回答
  •  失恋的感觉
    2020-12-13 06:56

    You can do this with GNU grep's perl mode:

    echo "12 BBQ ,45 rofl, 89 lol"|grep -P '\d+ (?=rofl)' -o
    

    -P means Perl-style, and -o means match only.

提交回复
热议问题