0

I have the following string:

PR-1769|7bb12d4152a497cef491e0a1088b3984ad92972f|jaga|

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

Also, how do I get the last string jaga using bash?

3
  • what's the pattern you're trying to match ? Commented Aug 12, 2016 at 23:22
  • You'll probably need regular expressions. Commented Aug 12, 2016 at 23:24
  • its usually PR-NUMBER|STRING|STRING| I need to get the NUMBER Commented Aug 12, 2016 at 23:24

3 Answers 3

2

There's no need for grep here: bash has built-in regex support, which is vastly more efficient to use than starting up an external utility just to process a single line:

re='^PR-([0-9]+)'
s='PR-1769|7bb12d4152a497cef491e0a1088b3984ad92972f|marvel|'

if [[ $s =~ $re ]]; then
  echo "Matched: ${BASH_REMATCH[1]}"
fi

You can also use parameter expansions:

s='PR-1769|7bb12d4152a497cef491e0a1088b3984ad92972f|marvel|'
s="${s%%'|'*}" # trim off everything after the first |
s="${s#PR-}"   # trim off the leading PR
echo "$s"

If you needed to extract the individual fields, by the way, read might be the correct tool for the job:

s='PR-1769|7bb12d4152a497cef491e0a1088b3984ad92972f|marvel|'
IFS='|' read -r pr hash username <<<"$s"

...the above will put PR-1769 into the variable pr, 7bb12d4152a497cef491e0a1088b3984ad92972f into the variable hash, and marvel into the variable username. To strip off the PR-, then, might simply look like:

echo "${pr#PR-}"

...or, to print the extracted username:

echo "$username"

See:

Sign up to request clarification or add additional context in comments.

1 Comment

Note as well that both the parameter expansion and read methods work in (non-bash) POSIX shells, although the read method requires a here document rather than bash's triple-< form.
0

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.

9 Comments

type grep --version and tell me the grep version
grep (BSD grep) 2.5.1-FreeBSD
...why do you need -P? These are strictly ERE-compatible regexes. (Not that I'd advise using grep here at all).
I have also found this to be strange. But certainly it is not working without -P
It needs -E to specify ERE, because otherwise you have BRE syntax, which doesn't support + in the way you expect, but you don't need -P.
|
0

It could be done using awk:

echo 'PR-1769|7bb12d4152a497cef491e0a1088b3984ad92972f|jaga|'|awk -F'|' '{print substr($1, 4)}'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.