0

I'm trying to make a script that redirects data from a serial port to other one.
I have realizate it using this command:

cat /dev/ttyS0 > /dev/ttyS1

Everything works but, now I would also logging data. I thought I'd use the tee command:   

cat /dev/ttyS0 | tee /dev/ttyS1 log.txt

Now I want to make sure that every time it is recorded on the log file should be preceded by the string "from S0 to S1:" I tried this:

cat /dev/ttyS0 | tee /dev/ttyS1 | sed 's/$/from S0 to S1/' | less > log.txt

But it does not work, the file remains empty. Where am I doing wrong?

devnull
  • 118,548
  • 33
  • 236
  • 227
  • Nitpick: looks like a useless use of cat; better: `tee /dev/ttyS1 < /dev/ttyS0`. – Jens Jun 26 '13 at 12:33
  • Are you sure there's never anything in log.txt? Note that pipes usually buffer I/O anywhere from 1kB to several kB. If that's the case, you need to be more patient. – Jens Jun 26 '13 at 12:34

2 Answers2

1

Try:

cat /dev/ttyS0 | tee /dev/ttyS1 | sed 's/^/from S0 to S1: /' | tee log.txt

Since you wanted to prefix the line with the string, the $ in your sed has been replaced by ^. The substituted output is sent to STDOUT that can serve as an input for tee.

devnull
  • 118,548
  • 33
  • 236
  • 227
0

Not sure if this helps, but I'd remove the pager from the pipeline and redirect the sed output directly to the file. Also, if you want to prepend text you need to match the beginning of a line (^) not the end of a line ($).

... | sed 's/^/from S0 to S1: /' > log.txt

Also, what does the input look like in the first place? Does it contain linebreaks that the pattern could match?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328