bash - 在 bash printf 格式中,如何在 muliples 占位符中使用相同的值
问题描述
我想将文件的行数放在 printf 字符串的两个占位符中
"%s lines: %s\n"
^ ^
|here and |here
所以我得到了两个wc -l
::
$ do_stuff() {
printf "%s ## lines: %5s\n" \
`cat $1 | wc -l` \
`cat $1 | wc -l`;
}
$ do_stuff ./lpm/modelisator.py
426 ## lines: 426
$
有用!
有没有像 python 那样只给字符串一个值的方法?::
In [1]: '{0} line {0}'.format(426)
Out[1]: '426 line 426'
解决方案
使用大括号扩展:
printf '%s lines: %s\n' "$(wc -l <"$1")"{,}
推荐阅读
- android - 多次运行后未找到默认活动
- linux - 如何在用户使用 Shell 脚本输入的日期之后打印月份中的某一天(星期一,星期二)?
- amazon-web-services - 如何使用 SES 主体和 IAM 角色主体编写 AWS 存储桶策略
- javascript - 我尝试推入嵌套数组,但如果没有正确保存?
- sql-server-express - 如何将密码添加到localdb
- angular - mat-form-field 上动态添加的提示标签仅在组件获得焦点后才可见
- uml - JHipster UML 和 JDL 的用例和区别
- .net - DotNet 命令行错误
- javascript - 页面重新加载后如何加载不同的 Javascript(广告)?
- vim - 选择相对范围,忽略无效的行号