简体   繁体   English

Sed - 替换包含括号、逗号和未指定数量的空格的文本字符串

[英]Sed - Replacing text string containing parentheses, commas and unspecified number of whitespaces

Assume an alphanumeric text string that contains a section comprising a keyword, parentheses, and commas as well as a line break and an unspecified number of whitespaces immediately following some or all of the commas.假设一个字母数字文本字符串包含一个包含关键字、括号和逗号的部分,以及紧跟在部分或全部逗号之后的换行符和未指定数量的空格。 How do I replace such a section from the text string with a simple comma in bash (preferentially using sed )?如何用bash中的简单逗号替换文本字符串中的这样一个部分(最好使用sed )?

Example:例子:

$ cat have.txt
foo (keyword(00001..00002),keyword(00003..00004),
   keyword(00005..00006),keyword(00007..00008)) foo 
$ cat want.txt 
foo (keyword(00001..00002,00003..00004,00005..00006,00007..00008)) foo

Attempt:试图:

$ sed 's/),keyword(/,/g' have.txt
foo (keyword(00001..00002,00003..00004),
   keyword(00005..00006,00007..00008)) foo

(And, yes, I know that whitespaces can be captured via [[:space:]] .) (而且,是的,我知道可以通过[[:space:]]捕获空格。)

With GNU sed:使用 GNU sed:

sed -z 's/),\s*keyword(/,/g' file

With -z , you will be able to match linebreaks, the \s* will match zero or more whitespace including those linebreaks.使用-z ,您将能够匹配换行符, \s*将匹配零个或多个空格,包括这些换行符。

To actually modify the file use实际修改文件使用

sed -z -i 's/),\s*keyword(/,/g' file
cat >> edrep.txt << EOF

2s/   //
%s/(//g
%s/)//g
%s/keyword//g
1s/00001/\(keyword\(00001/
2s/8/8))/
1,2j
wq
EOF

ed -s file.txt < edrep.txt

I know this one has already been answered, but I felt like posting this solution anyway.我知道这个问题已经得到解答,但我还是想发布这个解决方案。 People here will probably consider it hackish;这里的人可能会认为它很骇人听闻; but the point is that you can climb around text files like a monkey with ed, and I don't know of any other program which lets you do that to the same degree.但关键是你可以像猴子一样用 ed 爬上文本文件,而且我不知道有任何其他程序可以让你达到同样的程度。

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

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