You are not logged in.

#1 2010-04-09 06:06:35

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

[Solved] Bash: For loop output, replace line instead of append line.

The topic pretty much says it.  I've built a script that reads various settings and then loops every few seconds.  The script works well but I'd like to instead of writing to newline to have the new output replace the line of last output.  The script is real basic and is something like this:

while true; do
 grep "something" filename | cut -d " " -f 3
 sleep 5
done

I was wondering if anyone knew how to replace the last line when the new output arrives.

Last edited by Gen2ly (2010-04-09 23:31:08)


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#2 2010-04-09 06:54:24

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [Solved] Bash: For loop output, replace line instead of append line.

'sed -i' or '>' if it's the only line.

Edit: How do you read the output file? Maybe 'tail -1' would help? It reads only the last line, so appending the new line to the end of the file would be OK. You can then periodically remove the "history" file.

Last edited by karol (2010-04-09 07:03:56)

Offline

#3 2010-04-09 08:11:43

the_isz
Member
Registered: 2009-04-14
Posts: 280

Re: [Solved] Bash: For loop output, replace line instead of append line.

If I understand correctly what you want to do, you might consider using 'watch'.
See 'man watch' for details.

Offline

#4 2010-04-09 11:39:45

Bralkein
Member
Registered: 2004-10-26
Posts: 354

Re: [Solved] Bash: For loop output, replace line instead of append line.

Do you mean like an updating message on the terminal? You can do that sort of thing using escape codes and ANSI terminal escape sequences, I think. Is this example helpful?

for ((i=0;i<10;i++)); do 
echo -n $i
sleep 1
echo -ne "\x0d\E[2K"
done

Last edited by Bralkein (2010-04-09 11:40:31)

Offline

#5 2010-04-09 23:20:10

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: [Solved] Bash: For loop output, replace line instead of append line.

The escape codes does it: clears the line before the next input.  Nice work, Bralkein.  Thank you.


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#6 2010-04-10 02:19:39

Profjim
Member
From: NYC
Registered: 2008-03-24
Posts: 658

Re: [Solved] Bash: For loop output, replace line instead of append line.

Offline

#7 2010-04-10 03:26:22

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: [Solved] Bash: For loop output, replace line instead of append line.

Huh, interesting.  Hadn't thought about using tput.  The escape codes are working good for me now, but this is good reference for the future.  Thanks, Profjim.


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#8 2010-04-10 07:57:21

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [Solved] Bash: For loop output, replace line instead of append line.

tput is a lot slower than echo ESC, though.

Offline

#9 2010-04-10 13:48:54

Profjim
Member
From: NYC
Registered: 2008-03-24
Posts: 658

Re: [Solved] Bash: For loop output, replace line instead of append line.

Procyon wrote:

tput is a lot slower than echo ESC, though.

Yeah, that's the price of portability for different terminals. You have to decide whether it's worth it.

Even if you decide it's not worth it, it's good to know about tput so that you can interactively query the codes recognized by your own terminal (well, at least, according to its terminfo entry, which may not always be 100% accurate).

Or you could query just once for each of the relevant codes, and save it in a bash variable.

Last edited by Profjim (2010-04-10 14:07:46)

Offline

Board footer

Powered by FluxBB