Fedja Mladenovic
Fedja Mladenovic

Reputation: 41

How to capture multiple groups on one line

So I'm trying to practice regex with sed on linux. I've got these lines:

│      .--.     3..6 °C        │  _ /"".-.     6..8 °C        │  _ /"".-.     2..6 °C        │  _ /"".-.     0..4 °C        │
│  _ /"".-.     -2..+3 °C      │  _ /"".-.     1..5 °C        │   ,\_(   ).   1..4 °C        │   ,\_(   ).   -1..+2 °C      │
│     (   ).    -1..+1 °C      │     (   ).    -2..+2 °C      │     (   ).    -4..+2 °C      │     (   ).    -4..+2 °C      │

How can I extract only numbers and their sign? But every single on of them? I'm very sloppy with using groups and regexes, and any tip and explanation would help. I got this regex which can shorten the problem, but I still can't extract every single match. I hope it can help.

sed -n -E "/[+-]?[0-9]+\.{2}[+-]?[0-9]+/p"

I want my output to be 3,6 6,8 2,6 0,4 if done on first line, for second and third:

-2,+3 1,5 1,4 -1,+2

-1,+1 -2,+2 -4,+2 -4,+2

Upvotes: 1

Views: 257

Answers (1)

potong
potong

Reputation: 58381

This might work for you (GNU sed):

sed -E 's/([+-]?[0-9]+)\.\.([+-]?[0-9]+)/\n\1,\2\n/g;s/^[^0-9+-].*$/ /mg;s/^ |\n| $//g' file

Surround valid strings by newlines (convert .. to , at the same time).

Replace all non-valid strings by a space.

Remove the spaces at the front and end of the line and any introduced newlines.

N.B. The use of the m flag on the substitution command.

An alternative, is to work through the line from beginning to end removing non-valid characters:

sed -E 's/^/\n/;:a;s/\n([+-]?[0-9]+)\.\.([+-]?[0-9]+)/\1,\2 \n/;ta;s/\n./\n/;ta;s/ ?\n//' file

Upvotes: 1

Related Questions