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

前端 未结 7 986
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:52

    You can use rextract to extract using a regular expression and reformat the result.

    Example:

    [$] echo "12 BBQ ,45 rofl, 89 lol" | ./rextract '[,]([\d]+) rofl' '${1}'
    45
    

提交回复
热议问题