0

I am trying to extract the Diagram ID from a file which looks like

id DiaMatch(13) = Sector(A1x12, 5, 47) * Shift(k2, -k2+k1, [], k1, -p2+k1, []);
id DiaMatch(14) = Sector(A2x12, 5, 47) * Shift(k2, k1-k2, [], k1, -p2+k1, []);
id DiaMatch(15) = Sector(A1, 5, 47) * Shift(k2, -k2+k1, [], k1, -p1+k1, []);
id DiaMatch(16) = Sector(A2, 5, 47) * Shift(k2, k1-k2, [], k1, k1-p1, []);
id DiaMatch(01) = Sector(A3x12, 6, 111) * Shift(k2, p2-k1, [], k1, k1-k2+p1, []);
id DiaMatch(02) = Sector(A3, 6, 111) * Shift(k2, -k1+k2, [], k1, k1, []);
id DiaMatch(07) = Sector(A1, 6, 111) * Shift(k2, -k2, [], k1, p1-k1, []);
id DiaMatch(08) = Sector(A2, 6, 111) * Shift(k2, -k2, [], k1, -k1+p1, []);
id DiaMatch(09) = Sector(A3x12, 6, 123) * Shift(k2, k1, [], k1, k2, []);
id DiaMatch(10) = Sector(A2x12, 6, 63) * Shift(k2, p2-k2, [], k1, p2-k1+p1, []);
id DiaMatch(11) = Sector(A3, 6, 123) * Shift(k2, k1, [], k1, k2, []);
id DiaMatch(12) = Sector(A2, 6, 63) * Shift(k2, p1-k2, [], k1, p2-k1+p1, []);

I only want to extract the numbers 13,14,15 and so on and save in a new file, and other things to be excluded.

I am very new in shell and could not try anything without opening and reading the file

while read line;
do
        echo $line
done <jobs.yaml

Tanmoy Pati
  • 101
  • 3
  • Relevant: [Linux bash/grep extract word from matching string](https://stackoverflow.com/questions/69665960/linux-bash-grep-extract-word-from-matching-string) -- though personally I'd use awk or `[[ $string =~ $regex ]]` and `$BASH_REMATCH`. – Charles Duffy Jun 30 '23 at 16:08
  • BTW, `echo $line` is buggy; see [I just assigned a variable, but `echo $variable` shows something else!](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) – Charles Duffy Jun 30 '23 at 16:09
  • edited appropriately – Tanmoy Pati Jun 30 '23 at 16:45
  • 1
    While you're editing -- see [Why should I not upload images of code/data/errors?](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors); if we can't copy-and-paste your sample data as text, we can't check our answers. – Charles Duffy Jun 30 '23 at 16:46
  • BTW, this is also arguably duplicative of [Extract pattern from a string](https://stackoverflow.com/questions/11533063/extract-pattern-from-a-string) – Charles Duffy Jun 30 '23 at 16:51
  • If it's the numbers after DiaMatch, then with gnu grep: `grep -Po 'DiaMatch\(\K\d+' jobs.yaml >newfile` – jhnc Jun 30 '23 at 16:59
  • yes. I have managed to do it now – Tanmoy Pati Jun 30 '23 at 17:00
  • 1
    @jhnc, notably, that's not just any GNU grep but specifically one compiled with the optional libpcre dependency; so the availability of `-P` is not just version-specific but also distro-specific. – Charles Duffy Jun 30 '23 at 17:17
  • @CharlesDuffy do you know if that is common? Feels like having to caveat that bash may not support array variables – jhnc Jun 30 '23 at 17:38
  • @jhnc, I don't often see libpcre left out on full-size user-oriented distros, but I wouldn't assume its presence on any kind of embedded system (or security-oriented / locked-down system -- libpcre has [occasionally had vulnerabilities](https://www.cvedetails.com/vulnerability-list/vendor_id-3265/Pcre.html)) without checking. – Charles Duffy Jun 30 '23 at 17:43

1 Answers1

1
pattern='^id DiaMatch[(]([[:digit:]]+)[)]'
while IFS= read -r line; do
  [[ $line =~ $pattern ]] || continue
  echo "${BASH_REMATCH[1]}"
done <infile >outfile

See this running in an online sandbox at https://ideone.com/ISoFjY

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441