简体   繁体   English

以编程方式删除osx终端中2个字符之间的所有文本

[英]Programmatically delete all text between 2 characters in osx terminal

I have a thousand of txt files 我有一千个txt文件

1.txt
2.txt
3.txt

in each files, several times I have tags among my text: 在每个文件中,几次在文本中包含标签:

{somethinghere...blablabla} than the text I want to keep than again {somethinghere...blablabla}

I'm not very pratical in mac osx command line, can someone help me to write a command opening each file, parsing it, and deleting all text included by two "{"? 我在Mac osx命令行中不是很实用,有人可以帮助我编写一个命令来打开每个文件,对其进行解析并删除两个“ {”所包含的所有文本吗?

To be clear: 要清楚:

First of all I need to open each file, than parse the text. 首先,我需要打开每个文件,而不是解析文本。 When the loop finds a "{" it starts deleting till it founds a "}". 当循环找到“ {”时,它将开始删除,直到找到“}”为止。 When done parsing it saves and close the file. 解析完成后,将保存并关闭文件。 That's what I need to do. 这就是我要做的。

 $ sed -i.bak -e 's@{[^}]*}@@g' *.txt
  • -i.bak make a backup copy of each modified files. -i.bak制作每个修改文件的备份副本。 If you don't want backups, on OsX use -i'' (the quotes are not necessary on Linux) 如果您不想备份,请在OsX上使用-i'' (在Linux上不需要引号)
  • in substitutions, the delimiter can be another character than / , here I choose @ , so : s@<REGEX>@<REMPLACEMENT>@ (the basic form for substitutions are s/// ) 在替换中,分隔符可以是/以外的其他字符,这里我选择@ ,所以: s@<REGEX>@<REMPLACEMENT>@ (替换的基本形式是s///
  • In the regex, we search a litteral { and all but not a } with [^}] . 在正则表达式中,我们搜索litteral {所有,但不是}[^}] * means 0 or more occurences. *表示0次或多次发生。 Last, we search the closing } and we replace the matching part by nothing, so it delete what was matching 最后,我们搜索结束}然后将匹配的部分替换为空,因此它删除了匹配的部分
  • the g modifier @the end means not only one match but all g修饰符@the end不仅意味着一个匹配项,还意味着所有匹配项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM