-   Linux - Newbie (
-   -   How to avoid LF when redirecting output with Tee or > (

klaus-3140 07-09-2014 02:42 PM

How to avoid LF when redirecting output with Tee or >
I have a script (Bash) that goes through many many pictures and to see the progress I have a counter like the following (test program):

let "n=0"
while [ $n -le 15 ]
printf "%s\r" "$n of xxxx bla bla..." # \r = carriage return
sleep 1

echo # to print the final line

My terminal command is: $ | tee -a record.txt

in order to have a record of the actions of the program.

My problem (I guess) is that "tee" is adding a LF to the CR, i.e. I get a new line for every cycle of the loop so the record.txt becomes far too big.
IF I redirect directly from printf with > I only get one line (the final line).

Is there a way to avoid the added LF?

rknichols 07-09-2014 03:27 PM

You are not getting any LF characters in that file, just the same sequence of characters that is sent to the terminal. If you run "hexdump -c record.txt" you will see that. You seem to be under the mistaken impression that the CR character will somehow rewind that file back to the start so that the next line of output will overwrite what is already there. It does not, as you have discovered.

kentyler 07-09-2014 03:29 PM

Use this

| tr -d "\n"

klaus-3140 07-10-2014 07:50 AM

I have now found, that you are right rknichols; the LF's are not in the text file.
It seems that the LF's are introduced by the display program, because, it turns out, that "cat" displays the file OK (i.e. with CR and no LF), while "gedit" gives too many lines (i.e. with both CR and LF).
LibreOffice Writer on the other hand displays the file in a third way, as one long line, i.e. without both CR and LF.

My problem is now if there is a way to make the text file independent of the display program.

All times are GMT -5. The time now is 09:56 AM.