以下Linux(在Debian 8上)的sed命令无法正常工作。
我想在所有行的末尾添加以下字符串/行:
"{type master; file "/etc/bind/db.hello";};"
我尝试过的脚本:
sed 's/$/" {type master; file "\/etc\/bind\/db.hello";};/' test.txt | head
我不知道为什么上面的脚本不起作用。我的问题是将上述行/字符串附加到linux文本文件的所有行的末尾。
发自({test.txt
):
127.0.0.1 005.free-counter.co.uk
127.0.0.1 006.free-adult-counters.x-xtra.com
127.0.0.1 006.free-counter.co.uk
127.0.0.1 007.free-counter.co.uk
127.0.0.1 007.go2cloud.org
127.0.0.1 0075-7112-e7eb-f9b9.reporo.net
我希望得到:
127.0.0.1 "005.free-counter.co.uk" {type master; file "/etc/bind/db.hello";};
127.0.0.1 "free-adult-counters.x-xtra.com" {type master; file "/etc/bind/db.hello";};
127.0.0.1 "free-counter.co.uk" {type master; file "/etc/bind/db.hello";};
127.0.0.1 "007.free-counter.co.uk" {type master; file "/etc/bind/db.hello";};
127.0.0.1 "go2cloud.org" {type master; file "/etc/bind/db.hello";};
127.0.0.1 "0075-7112-e7eb-f9b9.reporo.net" {type master; file "/etc/bind/db.hello";};
但是我的命令却导致以下结果:
" {type master; file "/etc/bind/db.hello";};
" {type master; file "/etc/bind/db.hello";};ra.com
" {type master; file "/etc/bind/db.hello";};
" {type master; file "/etc/bind/db.hello";};
" {type master; file "/etc/bind/db.hello";};
" {type master; file "/etc/bind/db.hello";};et
答案 0 :(得分:0)
您的解决方案只需要正确地转义字符:
sed 's/$/ "{type master; file "\/etc\/bind\/db.hello";};/' test.txt
正如本杰明W.指出的那样,也许您需要先转换文件:
dos2unix text.txt
答案 1 :(得分:-1)
更新的答案。命令:
sed '/#/!s/\t\([^ ]*\)[\r]*$/ "\1" {type master; file "\/etc\/bind\/db.hello";};/'
应该工作。您需要使用\([^ ]*\)$
之类的内容捕获每行的最后一部分,然后使用"\1"
将其括在引号中。
开头的/#/!
会导致它忽略包含#
的任何行,而这些行似乎来自您的示例文件。
\t
告诉它它需要一个制表符,该制表符将由空格代替。 [\r]*
是最后一个可选的回车符,因此,无论是否已将sed
应用于文件,此dos2unix
命令都将起作用。回车符将被删除,但是如果要保留它,请在罚款\r
之前添加/
。