Results 1 to 2 of 2

Thread: How to stop sed behaving differently when stdout is not a tty?

  1. #1
    Join Date
    Jun 2016
    Beans
    1,989
    Distro
    Xubuntu 20.04 Focal Fossa

    How to stop sed behaving differently when stdout is not a tty?

    Xubuntu 18.04. To illustrate the issue:

    First run this in Terminal -
    Code:
    bash -c 'for i in $(seq 1 5);do sleep 1;echo "$i";done' | sed 's/^/!!/g'
    Notice how one line is printed every second. That's good.

    Now run this -
    Code:
    bash -c 'for i in $(seq 1 5);do sleep 1;echo "$i";done' | sed 's/^/!!/g' | cat
    Now it...waits until the end, then suddenly prints everything?

    And it's not cat causing this -
    Code:
    bash -c 'for i in $(seq 1 5);do sleep 1;echo "$i";done' | cat
    This second behavior of sed is hosing a script I'm trying to write. How to force the first behavior of sed, i.e. output lines in real time?

  2. #2
    Join Date
    Jun 2016
    Beans
    1,989
    Distro
    Xubuntu 20.04 Focal Fossa

    Re: How to stop sed behaving differently when stdout is not a tty?

    never mind, I think I found the answer: use sed -u

    From man sed -
    -u, --unbuffered

    load minimal amounts of data from the input files and flush the output buffers more often

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •