用 sed 插入 n 个重复字符 [英] Insert with sed n repeated characters
本文介绍了用 sed 插入 n 个重复字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从 mysql 查询创建打印输出文件,我在每个 TOTAL 字符串后插入一个分隔线:
Creating a printout file from a mysql query, I insert a separation line after every TOTAL string with:
sed -i /^TOTAL/i'-------------------------------------------------- ' file.txt
有没有更优雅的方式来重复 n 个-"字符而不是输入它们?
Is there any more elegante way to repeat n "-" characters instead of typing them?
例如,如果我不得不简单地生成一行而不查找/插入,我会使用:
For instance, if I had to simply generate a line without finding/inserting, I would use:
echo -$-{1..50} | tr -d ' '
但不知道如何用 sed 做类似的事情到文件中.
but don't know how to do something similar with sed into a file.
谢谢!
推荐答案
将两者结合起来即可:
sed -i /^TOTAL/i"$(echo -$___{1..50} | tr -d ' ')" file.txt
这篇关于用 sed 插入 n 个重复字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文