如何使用sed的w命令写入文件?

如何使用sed的w命令写入文件?

下面是我的测试。我该如何修复该错误?

ubuntu@u1804:~# sed --version
sed (GNU sed) 4.5
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Jay Fenlason, Tom Lord, Ken Pizzini,
and Paolo Bonzini.
GNU sed home page: <https://www.gnu.org/software/sed/>.
General help using GNU software: <https://www.gnu.org/gethelp/>.
E-mail bug reports to: <[email protected]>.
ubuntu@u1804:~#
ubuntu@u1804:~# cat test
11
aa
bb
cc
edde
xx
aa
bb
cc
edde
22
ubuntu@u1804:~# sed -r '/^[a-z]/{s/^.*$/(&)/; w result}' test
sed: -e expression #1, char 0: unmatched `{'
ubuntu@u1804:~#
ubuntu@u1804:~# sed -r '/^[a-z]/{s/^.*$/(&)/; p}' test
11
(aa)
(aa)
(bb)
(bb)
(cc)
(cc)
(edde)
(edde)
(xx)
(xx)
(aa)
(aa)
(bb)
(bb)
(cc)
(cc)
(edde)
(edde)
22
ubuntu@u1804:~#

答案1

检查您的工作目录,您可能有一个名为result}创建的文件

sed -r -e '/^[a-z]/{s/^.*$/(&)/; w result' -e '}' test

有不同的解决方法,其中之一是使用-e选项来分隔命令。当w使用时,它后面的所有字符都用于文件名,因此您会收到错误,因为{没有结束对

进一步阅读:GNU sed 手册:需要换行符的命令

相关内容