2

I have a server which sends data in a UDP Broadcast on port 10552. The data is 7 numbers separated by commas as so.

5.351204,0.001968,-0.000473,-0.999222,0.000451,0.001455,1.084310

I fetch this data with

socat -u udp-recv:10552,reuseaddr -

I want to change the format of this data, so I pipe it into sed. I don't care about the first number, so I remove it with

s/[^,]*,//;

I add spaces after the commas with

s/,/, /g

I then add an open parenthesis to the beginning of each line with

 s/^/(/

Finally, I attempt to add a close parenthesis to the end of each line with

s/$/)/

The final command ends up being

socat -u udp-recv:10552,reuseaddr - | sed 's/[^,]*,//;s/,/, /g;s/^/(/;s/$/)/'

Everything works as expected, except the final close parenthesis ends up on the beginning of each line, so it looks like

)0.051727, -0.595779, -0.794678, 0.082047, 0.644327, -0.027242

Removing the last bit of the command for

socat -u udp-recv:10552,reuseaddr - | sed 's/[^,]*,//;s/,/, /g;s/^/(/;'

results in

(0.079330, -0.608597, -0.786194, 0.092438, 0.657444, -0.031528

I have tried replacing appending the end of the line with awk, tr, and also GNU sed, but everything seems to overflow the line or something.

I am on OS X 10.10.3 with the builtin Bash 3.2. Is there anything else I can try?

2 Answers 2

1

socat is returning data with \r\n line endings. Change

s/$/)/

to

s/\r$/)/

That may or may not work with the OSX sed

1
  • That was it! Thanks! OS X sed doesn't like the carriage return, but you can either use GNU sed or preface the command with a $. sed $'s/\r$/)/' Apr 5, 2015 at 0:39
1

An alternative to explicitly removing the CR, is to tell socat that the lines are CR NL terminated:

 socat -u udp-recv:10552,reuseaddr,crnl - | sed 's/[^,]*,//;s/,/, /g;s/^/(/;s/$/)/'

You must log in to answer this question.

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