bash 用 sed 插入 n 个重复字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15072875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert with sed n repeated characters
提问by jm_
Creating a printout file from a mysql query, I insert a separation line after every TOTAL string with:
从 mysql 查询创建打印输出文件,我在每个 TOTAL 字符串后插入一个分隔线:
sed -i /^TOTAL/i'-------------------------------------------------- ' file.txt
Is there any more elegante way to repeat n "-" characters instead of typing them?
有没有更优雅的方式来重复 n 个“-”字符而不是输入它们?
For instance, if I had to simply generate a line without finding/inserting, I would use:
例如,如果我不得不简单地生成一行而不查找/插入,我会使用:
echo -$-{1..50} | tr -d ' '
but don't know how to do something similar with sed into a file.
但不知道如何用 sed 做类似的事情到文件中。
Thanks!
谢谢!
回答by that other guy
Just combine the two:
只需将两者结合起来:
sed -i /^TOTAL/i"$(echo -$___{1..50} | tr -d ' ')" file.txt
回答by Gilles Quenot
Another way using builtin printfand bash brace expansion:
使用内置printf和 bash大括号扩展的另一种方法:
sed -i "/^TOTAL/i $(printf '%.0s-' {0..50})" file.txt

