在Mac上的Sed不能识别正则表达式 [英] Sed on Mac not recognizing regular expressions

查看:208
本文介绍了在Mac上的Sed不能识别正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在终端中,我试图清理一些.txt文件,以便将它们导入到另一个程序中。只有文字搜索/替换似乎正在工作。

如果我尝试搜索并用文字字符串进行替换,它可以正常工作:



find。 -type f -name'* .txt'-exec sed -i's / Title Page // {} +;



(从每个文本文件中删除标题页字样)



但是,如果我正在尝试即使是最基本的正则表达式,它也不起作用:



find。 -type f -name'* .txt'-exec sed -i's / \\\
\\\
DOWN / \\\\



(在每个文本文件中,重新格式化任何符合双重返回的单词DOWN:删除多余的换行符,并将括号中的单词放在\ n)



这不起作用。关于这一切的唯一的正则表达式是寻找换行符。



我必须做一些不正确的事情。



任何帮助都非常感谢。



更新:第2部分



John1024的答案帮我解决了一个问题。



find。 -type f -name'* .txt'-exec sed -i'''/ ^ $ / {N; s / \\\
[0-9] + / \ n /;}'{} +;



让其他类型的正则表达式正确响应。上面的例子中,我希望删除出现在行首的所有数字。



呃!我缺少什么?

解决方案

默认情况下,sed一次只处理一行。当一行被读入sed的模式空间时,换行符将被删除。



我看到你想要查找一个空行,后跟 DOWN ,找到时,删除空白并将文本更改为< DOWN> 。这可以做到。考虑这个测试文件:

  $ cat file 
some
thing
DOWN


其他

试试:

  $ sed'/ ^ $ / {N; \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 
其他



工作原理




  • / ^ $ /



    这会查找空行。后面的花括号中的命令仅在空行上执行。



  • 命令将下一行读入模式空间,并与换行符分离。



    如果模式空间匹配空行,后跟 DOWN ,替换命令 s / \ nDOWN /< DOWN> / 删除换行符并替换 DOWN with < DOWN>




特殊情况:DOS / Windows文件



如果文件具有DOS / Windows行尾, \r\\\
,sed只会在读入行时移除 \\\
\r 将保持。在处理这些文件时,如果出现意外,该字符的出现可能会导致令人惊讶的结果。


In terminal, I am attempting to clean up some .txt files so they can be imported into another program. Only literal search/replaces seem to be working. I cannot get regular expression searches to work.

If I attempt a search and replace with a literal string, it works:

find . -type f -name '*.txt' -exec sed -i '' s/Title Page// {} +;

(remove the words "Title Page" from every text file)

But if I am attempting even the most basic of regular expressions, it does not work:

find . -type f -name '*.txt' -exec sed -i '' s/\n\nDOWN/\\n<DOWN\>/ {} +;

(In every text file, reformat any word "DOWN" that follows double return: remove extra newline and put word in brackets: "\n")

This does not work. The only thing at all "regular expression" about this is looking for the newline.

I must be doing something incorrectly.

Any help is much appreciated.

Update: part 2

John1024's answer helped me out a lot for one aspect.

find . -type f -name '*.txt' -exec sed -i '' '/^$/{N; s/\n[0-9]+/\n/;}' {} +;

Now I am having trouble getting other types of regular expressions to respond properly. The example above, I wish to remove all numbers that appear at the beginning of a line.

Argh! What am I missing?

解决方案

By default, sed handles only one line at a time. When a line is read into sed's pattern space the newline character is removed.

I see that you want to look for an empty line followed by DOWN and, when found, remove the empty and change the text to <DOWN>. That can be done. Consider this as the test file:

$ cat file
some
thing
DOWN

DOWN
other

Try:

$ sed '/^$/{N; s/\nDOWN/<DOWN>/;}' file
some
thing
DOWN
<DOWN>
other

How it works

  • /^$/

    This looks for empty lines. The commands in braces which follow are executed only on empty lines.

  • {N; s/\nDOWN/<DOWN>/;}

    The N command reads the next line into the pattern space, separated from the current line by a newline character.

    If the pattern space matches an empty line followed by DOWN, the substitution command, s/\nDOWN/<DOWN>/, removes the newline and replaces the DOWN with <DOWN>.

Special Case: DOS/Windows Files

If a file has DOS/Windows line endings, \r\n, sed will only remove the \n when the line is read in. The \r will remain. When dealing with these files, the presence of that character, if unanticipated, may lead to surprising results.

这篇关于在Mac上的Sed不能识别正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆