Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Comments on How to overwrite each line of STDOUT with the next one?

Parent

How to overwrite each line of STDOUT with the next one?

+3
−0

I wrote a filter program to overwrite each line with the next one.

$ cat /usr/local/bin/ovr
#!/bin/sh

sed '2,$s/^/\x1B[1A\x1B[K/';

Here's the behavior:

$ echo | ovr

$ echo 'foo' | ovr
foo
$ echo -e 'foo\nbar' | ovr
bar
$ echo -e 'foo\nbar\nbaz' | ovr
baz
$ echo -e 'foo\nbar\nbaz' | ovr | wc -l
3

It is useful for example when compiling a large program, where you might not want to pollute your screen with so many lines. For example, in build systems like the Linux kerenl one, for each file that's compiled, one line goes to stdout, of the form CC some/file.o.

I usually compile it with make -j24 | ts -s, to know for how long it's been running, from which I can guess how much is remaining. But I don't really need to see all lines; just the last one. So I do the following.

$ time make -j24 | ts -s | ovr
00:00:04   CC      drivers/pinctrl/intel/pinctrl-sunrisepoint.o

The line will be updating all the time.

This program has a some flaw, which I'd like to fix.

If the line is long (longer than the terminal width), the overwriting doesn't work well, and only overwrites the part of the lines that has been written in the last "visual" line in the terminal.

For an 80-col terminal, this is an example:

$ echo -e '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o\nfoo' | ovr
123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789ifoo

How can this bug be fixed? Is it possible to fix it?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+2
−0

In Bash, you could use the $COLUMNS environment variable to detect the width of your terminal and truncate each line to that length in your sed script. Something like this should work:

sed "s/^\(.\{,$COLUMNS\}\).*$/\1/;2,\$s/^/\x1B[1A\x1B[K/"

For a sh-compatible alternative, replace $COLUMNS with $(tput cols).

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

$COLUMNS is not available in a program, but `tput cols` is (4 comments)
$COLUMNS is not available in a program, but `tput cols` is
alx‭ wrote about 1 year ago · edited about 1 year ago

Thanks!

Just a minor nitpick: $COLUMNS is not available (unless I export it manually). I replaced it with tput cols:

$ cat /usr/local/bin/ovr 
#!/bin/sh

sed "s/^\(.\{,$(tput cols)\}\).*$/\1/;2,\$s/^/\x1B[1A\x1B[K/";

(see https://stackoverflow.com/a/263900)

Kamil Maciorowski‭ wrote about 1 year ago

The answer does not state $COLUMNS is not portable. It works in "big" shells (Bash, Zsh) but sh is not required to support it.

r~~‭ wrote about 1 year ago

Fair, post edited.

Kamil Maciorowski‭ wrote about 1 year ago

Another thing: each tab character in the input will count as one, but in general the terminal will move the cursor more and the truncated line will look longer than $COLUMNS and still wrap (see man 1 expand, this may help). On the other hand there may be unprintable characters that may cause the truncated line look shorter than $COLUMNS.