1

is there a way to do line-bufferd cat? For example, I want to watch a UART device, and I only want to see it's message when there is a whole line. Can I do some thing like:

cat --line-buffered /dev/crbif0rb0c0ttyS0

Thanks.

Jinghao Shi
  • 1,077
  • 2
  • 10
  • 15

3 Answers3

4

You can also use bash to your advantage here:

cat /dev/crbif0rb0c0ttyS0 | while read line; do echo $line; done

Since the read command reads a line at a time, it will perform the line buffering that cat does not.

Greyson
  • 3,598
  • 1
  • 21
  • 22
3

No, but GNU grep with --line-buffered can do this. Just search for something every line has, such as '^'.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • I tried `cat /dev/crbif0rb0c0ttyS0 | grep . --line-buffered`, but only empty lines are printed. If I substitute the UART device with a normal file, then it's OK. Any ideas on this? – Jinghao Shi May 04 '12 at 06:27
  • Oh, I tried `cat /dev/crbif0rb0c0ttyS0 | grep ^ --line-buffered`, and this works! Don't know why though... – Jinghao Shi May 04 '12 at 06:34
  • This works but can you explain? cat's output supposed to be buffered since it is connected to a pipe. So how the grep's --line-buffered couses the cat's buffer to flush in order for grep to operate as line-buffered? – Vassilis Oct 05 '12 at 22:26
  • 1
    "cat's output supposed to be buffered since it is connected to a pipe." [citation needed] – Ignacio Vazquez-Abrams Oct 05 '12 at 23:11
  • I came across this while doing a little research about stdio buffers. There is [this](http://www.pixelbeat.org/programming/stdio_buffering/) article, and I in many situations is being mentioned that _...By pretending to be a terminal, the buffering of stdout is automatically set to 'line mode, otherwise it is set to buffered (4KB usually)...'_. I am not sure though, so if you could please give some technical reference about stdout, stdin, etc buffering behavior along with pipes, it would be very helpful. Thanks... – Vassilis Oct 06 '12 at 12:20
1

Pipe it through perl in a no-op line-buffered mode:

perl -pe 1 /dev/whatever
bdonlan
  • 224,562
  • 31
  • 268
  • 324