[英]How to remove specific “][” in linux
I have text files with lots of brackets as below:我有很多括号的文本文件,如下所示:
Name:[Sara,Jane,John]
FamilyName: [Alexandr,Walter,Waze]
Number: [21][44]
I want to just remove ][ in Number and make the file look like as below:我只想删除 Number 中的 ][ 并使文件如下所示:
Name:[Sara,Jane,John]
FamilyName: [Alexandr,Walter,Waze]
Number: [2144]
Below Code remove all brackets and cant work properly, I really if any can help me in modifying this code?下面的代码删除了所有括号并且无法正常工作,我真的可以帮助我修改此代码吗?
find . -name "*.txt" |xargs sed -i.bak -e 's/[][]//g;' '{}'
Best Regards, Sara最好的问候,萨拉
$ sed '/Number/ s/]\[//' ip.txt
Name:[Sara,Jane,John]
FamilyName: [Alexandr,Walter,Waze]
Number: [2144]
/Number/
match lines containing Number
/Number/
匹配包含Number
的行
s/]\[//
will remove first occurrence of ][
s/]\[//
将删除第一次出现的][
[
needs to be escaped to match it literally [
需要转义以匹配它[][]
will match ]
or [
character once. [][]
将匹配]
或[
字符一次。 Use of g
flag will then remove all square brackets.然后使用
g
标志将删除所有方括号。 In this example, you shouldn't use character class because you want to match ]
and [
in that particular order.在此示例中,您不应使用字符 class,因为您希望以特定顺序匹配
]
和[
。
To give another example, to delete foo
, you need to use s/foo//
and not s/[foo]//
.再举一个例子,要删除
foo
,您需要使用s/foo//
而不是s/[foo]//
。
Could you please try following, based on your shown examples only.您能否仅根据您显示的示例尝试以下操作。
awk '/Number/{gsub(/]\[/,"")} 1' Input_file
Explanation: /Number/
is looking for string Number
in current line and if found;解释:
/Number/
正在寻找当前行中的字符串Number
,如果找到; then using gsub
globally substituting ][
with NULL.然后使用
gsub
将][
全局替换为 NULL。 1
will print current line. 1
将打印当前行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.