Can I overwrite output to the screen with awk?

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?

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

bash - Can I overwrite output to the screen with awk?, 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​  Hi guys, I checked the knowledge base before posting this question. is there any way by which you can ALWAYS ALLOW file overwrite in AWK?. i.e. an option similar to noclobber in Korn shell.

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.

One Tip Per Day: awk redirecting output in AWK: override or not?, Subsequent writes to the same output-file do not erase output-file , but For example, here is how an awk program can write a list of BBS I do it in my terminal it prints it on the top frame of the window but nothing is…”. A redirection appears after the print or printf statement. Redirections in awk are written just like redirections in shell commands, except that they are written inside the awk program. There are four forms of output redirection: output to a file, output appended to a file, output through a pipe to another command, and output to a coprocess. We

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.

Multiple File Redirection with awk and find, With GNU awk 4.1 or above: find . -type f -exec awk ' @load "inplace" BEGINFILE { inplace_begin(FILENAME, "") print "line1\nline2" } {print}  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 (LF) is used, and \r when used means solely to return the character to the start of the current line.

Learning AWK Programming: A fast, and simple cutting-edge utility , Till now we have been sending the output of print and printf commands to stdout, that is, the screen. However, we can also redirect the output to files by using the  print items > output-file This redirection prints the items into the output file named output-file.The file name output-file can be any expression.Its value is changed to a string and then used as a file name (see Expressions).When this type of redirection is used, the output-file is erased before the first output is written to it.

Linux and UNIX Shell Programming, Also it will overwrite what was in the file previously. The second way is to use the tee command, which will show the output on the screen as well as going to a  Then I get output on terminal screen but I can't redirect to (or overwrite) all files. So, I tried following (To find + awk): find -type f -exec awk 'BEGIN { print "line1 line2" } { print $0 } END { print "line3 line4" }' '{}' \; By using above command I can print output on screen and hence to overwrite files,

Computational Biology: Unix/Linux, Data Processing and Programming, Usually, print prints to the standard output, that is the screen. However, you can also use redirections in order to print into a file. awk 'print If it exists, it will be overwritten, unless you use >>, which appends the output to an existing file. Now let  However I found screen didn't accpet redirection. for example. screen -dmS name ls>ls.dat won't generate ls.dat. Fortunately, screen -L will output screen's log to a file. However, what it does is to append to previous log file, even if I pkill screen and start a fresh new screen.

Comments
  • 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.
  • Good one! (upvoted before). Related read: Clear the Ubuntu bash screen for real
  • 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.
  • That's indeed a workaround with the advantage of not truncating the processed lines. Clearing the screen might be an issue though.
  • 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 Why do you like to pipe escape characters to a file?? This is just to control the output to the screen.