I'd like to track the progress of an awk command operating over many files. At the moment I'm printing the file name to the screen with each print statement, but I'd rather not flood the screen. Rather than print to a new line each time, I was hoping to write to the same line over and over. Is this possible?
-
It would be useful if you gave some more context, explaining what you're trying to do exactly and showing your script. There may be other ways to deal with this problem. – Tom Fenech Oct 16 '14 at 13:33
3 Answers
You can use ANSI Escape sequences
with awk
. Try this:
seq 1 100000 | awk '{print $1 "\033[1A"}'
Esc[ValueA
Cursor Up:
Moves the cursor up by the specified number of lines without changing columns. If the cursor is already on the top line, ANSI.SYS ignores this sequence.
To solve problem raised by Jlliagre you can do:
seq 100000 -1 1 | awk '{print "\033[2J\033[;H" $1}'
It clears the screen and sets the location of the cursor to position 0,0

- 40,548
- 12
- 51
- 55
-
Good one! (upvoted before). Related read: [Clear the Ubuntu bash screen for real](http://stackoverflow.com/a/5367075/1983854) – fedorqui Oct 16 '14 at 10:50
-
There are two issues with this approach. If the string to display is wider than the terminal width, extra lines will stay on the screen. If a string to display is shorter than a previous one (eg: time seq 100000 -1 1), leftover characters will be displayed after it. – jlliagre Oct 16 '14 at 10:55
-
That's indeed a workaround with the advantage of not truncating the processed lines. Clearing the screen might be an issue though. – jlliagre Oct 16 '14 at 12:06
-
I got an empty screen with the second command. With the first one, piping this to a file will get you the sequence list with the control characters. To make sure the terminal sees the same thing as a file, it is vital to not rely on escape sequences, but to format the output beforehand. – joepd Oct 16 '14 at 18:00
-
@joepd Why do you like to pipe escape characters to a file?? This is just to control the output to the screen. – Jotne Oct 16 '14 at 20:35
-
@Jotne: While I tried being a smartass by using the backspace character to delete a trailing comma from a list that I generated in a loop. Works beautifully in the terminal. When trying to use the result, however, not so much :) I believe that display in terminal should reflect the real text produced, as a small implication of the UNIX philosophy. Because, inevitably, if this is a useful thing, someone will pipe it to a file some day... – joepd Oct 16 '14 at 20:59
-
@Jotne In this particular problem I don't want to clear the entire screen and the file names are all 22 characters long, so your first solution seems to be what I was looking for. Thanks! – user3719139 Oct 17 '14 at 17:43
I'm submitting an alternative which does pretty much the same as the accepted answer but with less voodoo:
awk '{ printf("\r" $0) }'
\r
is the "Carriage Return" - in Windows (MS-DOS) land it's one of the two characters used to denote a new line (CRLF) - in Unix only \n
(LF) is used, and \r
when used means solely to return the character to the start of the current line.
So this example just repeats input but starting from the start of the same line each time (unlike print
, there is no implicit newline emitted by printf
in awk).
So for a toy example, keep updating the time on the same line each second:
while sleep 1; do date ; done | awk '{ printf("\r" $0) }'
I can't say for sure but maybe this is a more widely compatible solution in that it doesn't rely on ANSI terminal feature? Maybe terminal support for carriage return is predicated on ANSI escape anyway ... I think in the degenerate case it will print less gobbledygook to the screen however.

- 4,612
- 2
- 29
- 39
Here is one way to do it:
find / -type f 2>/dev/null | \
awk -v c=${COLUMNS:-80} '{ printf("%-*.*s \r",c-1,c-1,$0);}'
The printf command is truncating the displayed string to keep the output in a single line, overwritten by the next one.
If you know the maximum width of your output, you can use it instead of the COLUMN variable. This will make the process much faster.

- 29,783
- 6
- 61
- 72