击的printf preFIX [英] Bash printf prefix

查看:137
本文介绍了击的printf preFIX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在猛砸我可以使用的printf 来格式化像一个字符串输出如下: - (请注意我是如何加入的后缀是W 来字符串和,这是不包括在填充)

In Bash I can use printf to format a string output like this:- (Note how I've added a suffix of W to the string and that this is not included in the padding)

$ printf "Blah %11.1fW\n" 123 456 78965 5 56
Blah       123.0W
Blah       456.0W
Blah     78965.0W
Blah         5.0W
Blah        56.0W

如果我想preFIX字符串我可以这样做: -

If I want to prefix the string I could do this:-

$ printf "Blah £%11.1f\n" 123 456 78965 5 56
Blah £      123.0
Blah £      456.0
Blah £    78965.0
Blah £        5.0
Blah £       56.0

但是注意,这导致填充如何preFIX之前被应用。

However note how this results in the padding being applied before the prefix.

如何(如果可能的话)我会使用的printf 来preFIX值填充之前,所以输出将如下所示: -

How (if at all possible) would I use printf to prefix the value before padding so the output would be as follows:-

Blah      £ 123.0
Blah      £ 456.0
Blah    £ 78965.0
Blah        £ 5.0
Blah       £ 56.0

如果不可能任何击的解决方案将是适当的。

If not possible any Bash solution would be appropriate.

推荐答案

我想出了这一点:

$ printf "Rate: %11s\n" $(printf "$%.1f " 12345 123)
Rate:    $12345.0
Rate:      $123.0

只是要确保选择正确的%11S 为你的情况。

看来你自己解决了空间问题。为了完整起见,我会把在这里。

It seems that you addressed that space problem yourself. For the sake of completeness, I will put that in here.

$ printf "Rate: %11s\n" $(printf "$%.1f " 12345 123) | sed 's/\$/\$ /g'
Rate:    $ 12345.0
Rate:      $ 123.0


下面是由@FatalError给出的解决方案:


Here's the solution given by @FatalError:

printf "Rate: %11b\n" $(printf '$\\0040%.1f ' 12345 123)
Rate:   $ 12345.0
Rate:     $ 123.0

这篇关于击的printf preFIX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆