I have a program that reads from a sensor and prints on stdout. The interesting data for me is just the X,Y and Z values:

[2021-05-07 10:47:41.993] [info] Acceleromter (avrg 100): X=+6696.080 Y=-5577.230 Z=-5813.550
[2021-05-07 10:47:42.249] [info] Acceleromter (avrg 100): X=+6692.000 Y=-5580.190 Z=-5808.080
[2021-05-07 10:47:42.506] [info] Acceleromter (avrg 100): X=+6694.200 Y=-5582.050 Z=-5811.460
[2021-05-07 10:47:42.763] [info] Acceleromter (avrg 100): X=+6690.390 Y=-5578.800 Z=-5812.100
[2021-05-07 10:47:43.020] [info] Acceleromter (avrg 100): X=+6691.930 Y=-5580.140 Z=-5808.930
[2021-05-07 10:47:43.277] [info] Acceleromter (avrg 100): X=+6693.400 Y=-5578.180 Z=-5805.930

I redirect the output to a file:

$program >> data.txt

Now I want to extract it to another file like this:

+6693.400 -5578.18 -5805.930

That is simply values of X, Y and Z with either space or tab as delimiter.

I tried with cut, awk and some other things but I absolutly have no idea what am I doing... I am just bad with these stuff. any help would be great.

  • 1
    I can think of grep -Po 'X=[+-][0-9]*\.[0-9]* Y=[+-][0-9]*\.[0-9]* Z=[+-][0-9]*\.[0-9]*' | sed 's/[XYZ]=//g'
    – Tom Yan
    May 7, 2021 at 9:16

2 Answers 2


I assume spaces (or tabs) and = characters cannot appear "randomly" and therefore they are good separators. If so then this awk command will work:

awk -F '[\t =]' '{print $8 OFS $10 OFS $12}'

The default OFS is space. In this form the processing is purely textual, so from -5578.180 you get -5578.180, not -5578.18.

(There are ways to get rid of trailing zeros. Your desired output omits a trailing zero in the second value (Y) but not in other values, so I'm not sure if you really want this. I'm keeping the answer simple.)

Use the awk command like this: program | awk …; or like this: <data.txt awk ….


With Gawk :

awk -F'[XYZ]=' '{print $2 $3 $4}' data.txt

here FS is X=, Y= or Z=.
assume they can't been in the first part of the data.

With sed :

sed -E 's/.*((.=.*){3})/\1/;s/.=//g' data.txt

first keep only the X=..., Y=...
after that, keep only the data.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .