Federico Massimi
Federico Massimi

Reputation: 291

Shell: sed pipeline

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?

Upvotes: 0

Views: 7149

Answers (2)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200413

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?

Upvotes: 0

devnull
devnull

Reputation: 123608

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.

Upvotes: 1

Related Questions