How can I parse a string using bash to get a number

后端 未结 3 1004
轻奢々
轻奢々 2021-01-28 07:51

I have the following string:

PR-1769|7bb12d4152a497cef491e0a1088b3984ad92972f|jaga|

How can I parse the string above using bash so that I get t

3条回答
  •  耶瑟儿~
    2021-01-28 08:25

    Based on posted,

    by using grep

    echo 'PR-1769|7bb12d4152a497cef491e0a1088b3984ad92972f|marvel|' | grep -Po "(PR-)([0-9]+)" | grep -Po "[0-9]+"
    
    1. the string is being echoed and redirected to grep for the first time

    2. in first iteration grep is extracting and outputting PR-1769

    3. PR-1769 is in second iteration redirected to grep again, now 1769 is extracted and outputted


    This is only first-shot version of solution, some grep regex expert would certainly know how to do it with single grep call.

    It will only give you result if echoed string is in PR-NUMBER format, as you have presented.

提交回复
热议问题