【问题标题】:Adding a command at the beginning of every line using bash command [duplicate]使用 bash 命令在每行的开头添加一个命令 [重复]
【发布时间】:2019-03-09 10:11:01
【问题描述】:

如何在每行示例的开头添加一个命令-

我的文件包含以下文本 -

abc
efg
hij

所以我想添加这个sed -n '/^<<</,/^>>>/p'

sed -n '/^<<</,/^>>>/p' abc
sed -n '/^<<</,/^>>>/p' efg
sed -n '/^<<</,/^>>>/p' hij

我试图运行这个命令

sed -i 's/^/sed -n '/^&lt;&lt;&lt;/,/^&gt;&gt;&gt;/p'' test.txt 但它不起作用。

有人可以帮忙吗?

【问题讨论】:

  • 试试这个:sed -i "s#^#sed \-n \'/\&lt;\&lt;\&lt;/\\,/\^\&gt;\&gt;\&gt;/p\' #" test.txt
  • 感谢@User123 这对我来说非常适合。

标签: regex bash shell


【解决方案1】:

您可以为此使用 awk:

$ cat test 
abc
def
ghi
$ awk '{print "sed -n \x27/^<</,/^>>/p\x27 " $0}' test
sed -n '/^<</,/^>>/p' abc
sed -n '/^<</,/^>>/p' def
sed -n '/^<</,/^>>/p' ghi

我很确定在打印中使用' 可以比\x27 具有更好的风格,但我现在对此没有一个好主意。

【讨论】:

  • 谢谢@Szczerba 这很好用,但我会选择 sed -i "s#^#sed \-n \'/\\ >\>/p\' #" test.txt 这是 User123 推荐的,因为这样可以节省我创建新文件的步骤。
猜你喜欢
  • 2018-09-04
  • 1970-01-01
  • 2014-06-10
  • 1970-01-01
  • 2017-12-20
  • 1970-01-01
  • 1970-01-01
  • 2018-10-28
相关资源
最近更新 更多