Issue
I need to print [PR:XXXXX] only.
Ex: [Test][PR:John][Finished][Reviewer:SE]
to [PR:John]
only. (PR tag)
Note:Other strings rather than the [PR:XXXXX] may changed time to time Ex:
[Test][PR:Cook][Completed]
[Test][Finished][PR:Russell][Reviewer:SE]
[Dump][Reviewer:SE][Complete][PR:Arnold]
Note: There are no multi line inputs and only one PR tag is included in all of inputs.
Untill I create following sed command but it did not work:
sed "s/\[PR:[^]]*\]//"
Solution
You might use bash for this:
s='[Test][PR:Cook][Completed]'
regex='\[PR:[^]]*]'
[[ "$s" =~ $regex ]] && echo "${BASH_REMATCH[0]}"
# => [PR:Cook]
See this online demo.
You may use grep
:
grep -o '\[PR:[^]]*]'
See this demo.
Or, you can use this sed
:
sed -n 's/.*\(\[PR:[^]]*]\).*/\1/p'
See this online demo.
Or, you can use awk
awk 'match($0,/\[PR:[^]]*]/) {print substr($0,RSTART,RLENGTH)}'
See the online demo.
Answered By - Wiktor Stribiżew