如何更改 AWK 中现有的打印行

发布于 2024-10-19 17:14:53 字数 199 浏览 1 评论 0原文

当我执行以下行时,它会打印换行符中的单词。

awk 'BEGIN { print "line one\nline two\nline three" }'

如何在同一行中打印信息并刷新现有行?

例如,在执行循环时,它应该打印“一”,然后擦除该行并打印“二”,然后擦除该行并打印“三”等。您可以帮助我吗?

When i execute the following line, its prints the words in newline.

awk 'BEGIN { print "line one\nline two\nline three" }'

How can i print the info in the same line with flush the existing line?

For example, while executing the loop, it should print 'one' then wipe out the line and prints 'two' then wipe out the line and prints 'three' etc. can you please assist me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪冰清 2024-10-26 17:14:53

尝试将换行符 \n 更改为回车符 \r (这是否有效取决于您的终端设置)。哦,打印一堆尾随空白来擦除前一行,并使用 printf 来避免 print 总是自动出现 \n补充道。

$ awk 'BEGIN { printf "line one    \r" }'; sleep 2; awk 'BEGIN {printf "line two   \r" }'

Try changing the newlines \n to carriage returns \r (whether this works depends somewhat on your terminal settings). Oh, and print a bunch of trailing whitespace to wipe-out the previous line, and use printf to avoid the automatic \n that print always adds.

$ awk 'BEGIN { printf "line one    \r" }'; sleep 2; awk 'BEGIN {printf "line two   \r" }'
少跟Wǒ拽 2024-10-26 17:14:53

通过“擦掉线路”,我假设您指的是马车return 可以使用 \r 转义序列来表示。

例如:

[me@home]$ awk 'BEGIN { print "line one\rline two\rline three" }'
line three

“第一行”和“第二行”消失,因为它们已被删除并被“第三行”取代。

如果您希望文本在被擦除之前在一段时间内可见,则需要在每次打印之间插入延迟。

awk 'BEGIN{
    printf "line one";
    system("sleep 1");
    printf "\rline two";
    system("sleep 1");
    printf "\rline three";
}'

或者,使用循环

echo "one two three" | awk '{
   for (i=1; i<=NF; i++) {
       printf "\rline %s", $i;
       system("sleep 1");
   }
}'

By "wipe out the line", I'm assuming you were referring to a carriage return which can be expressed using the \r escape sequence.

For example:

[me@home]$ awk 'BEGIN { print "line one\rline two\rline three" }'
line three

"line one" and "line two" disappear as they have been wiped out and replaced by "line three".

If you wish to have the text visible for a certain time before it gets wiped out, you'll need to insert delays in between each print.

awk 'BEGIN{
    printf "line one";
    system("sleep 1");
    printf "\rline two";
    system("sleep 1");
    printf "\rline three";
}'

or, using a loop

echo "one two three" | awk '{
   for (i=1; i<=NF; i++) {
       printf "\rline %s", $i;
       system("sleep 1");
   }
}'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文