一些文本在左边,一些文本在右边,在一行上,带有BASH
我在BASH脚本中显示了一些状态文本,例如:一些文本在左边,一些文本在右边,在一行上,带有BASH,bash,Bash,我在BASH脚本中显示了一些状态文本,例如: Removed file "sandwich.txt". (1/2) Removed file "fish.txt". (2/2) 我希望进度文本(1/2)完全显示在右侧,与终端窗口的边缘对齐,例如: Removed file "sandwich.txt". (1/2) Removed file "fish.txt". (2/2) 我在
Removed file "sandwich.txt". (1/2)
Removed file "fish.txt". (2/2)
我希望进度文本(1/2)
完全显示在右侧,与终端窗口的边缘对齐,例如:
Removed file "sandwich.txt". (1/2)
Removed file "fish.txt". (2/2)
我在尝试过解决方案,但是,这些解决方案似乎不起作用,它们只会产生一个大的空白,例如:
Removed file "sandwich.txt". (1/2)
Removed file "fish.txt". (2/2)
如何使部分文本左对齐,部分文本右对齐
printf "Removed file %-64s (%d/%d)\n" "\"$file\"" $n $of
文件名周围的双引号是折衷的,但将文件名括在printf()
命令的双引号中,然后该命令将在宽度为64的字段中左对齐打印该名称
调整以适应
$ file=sandwich.txt; n=1; of=2
$ printf "Removed file %-64s (%d/%d)\n" "\"$file\"" $n $of
Removed file "sandwich.txt" (1/2)
$
这将自动调整到您的终端宽度,无论是什么
[ghoti@pc ~]$ cat input.txt
Removed file "sandwich.txt". (1/2)
Removed file "fish.txt". (2/2)
[ghoti@pc ~]$ cat doit
#!/usr/bin/awk -f
BEGIN {
"stty size" | getline line;
split(line, stty);
fmt="%-" stty[2]-9 "s%8s\n";
print "term width = " stty[2];
}
{
last=$NF;
$NF="";
printf(fmt, $0, last);
}
[ghoti@pc ~]$ ./doit input.txt
term width = 70
Removed file "sandwich.txt". (1/2)
Removed file "fish.txt". (2/2)
[ghoti@pc ~]$
您可以删除开始块中的打印
;那只是用来显示宽度的
要使用此方法,基本上只需通过awk脚本对任何现有状态行进行管道创建,它会将最后一个字段移动到终端的右侧。“第二个解决方案并不总是使右列与终端的右边缘对齐”这就是为什么第一个解决方案如此复杂的原因;这个问题是关于右对齐终端的边缘,而不仅仅是如何使用printf。