Saturday, March 12, 2022

[SOLVED] In Bash is there a way to extract a word and n characters after it from a line?

Issue

I am trying to extract the JIRA Ticket number from a string.

The Jira ticket might be mentioned any where in the line like:

  1. Merge pull request #1387 from Config-change/REL-12345

  2. REL-12345: Enable XAPI at config level

I just want REL-12345 as the output.

Can someone please help. Thanks!


Solution

grep -Eow 'REL-[0-9]+'

+ is one or more, to specifiy N numbers (eg 5):

grep -Eow 'REL-[0-9]{5}
  • Ranges: {3,6} is 3 to 6, {5,} is 5 or more, etc.
  • On GNU/Linux: man grep -> /Repetition for more details.
  • -o prints only matching strings
  • -w matches full words only, ie. to avoid matching WREL-12345 (for example)
  • grep -Eow 'REL-[[:alnum:]]+' for both letters and numbers (after REL-).


Answered By - dan
Answer Checked By - Mary Flores (WPSolving Volunteer)