从特定字符串中删除括号

时间:2020-10-30 14:12:22

标签: awk sed

如果某些文字是喜欢的

makemigrations

我想要输出为

cell (ABC)
(A1)
(A2)
function (A1.A2)

我想从文件的每一行中删除括号,但功能行中的内容除外。 使用代码

cell ABC
A1
A2
function (A1.A2)

从每行中除去括号。如何修改上面的代码以获得所需的输出。

2 个答案:

答案 0 :(得分:7)

您可以在sed命令中添加跳出条件:

sed '/^function /b;s/[()]//g' file

或者,将替换条件设置为不匹配功能:

sed '/^function /!s/[()]//g' file

答案 1 :(得分:6)

能否请您尝试以下。用GNU awk中显示的示例编写和测试。

awk '!/function/{gsub(/[()]/,"")} 1' Input_file

说明: 添加以上详细说明。

awk '                ##Starting awk program from here.
!/function/{         ##Checking condition if line does not have function in it then do following.
  gsub(/[()]/,"")    ##Globally substituting ( OR ) with null in current line.
}
1                    ##1 will print current line.
' Input_file         ##Mentioning Input_file name here.