简体   繁体   English

使用sed查找/替换不适用于整个文本文件

[英]Using sed to find/replace doesn't work on whole text file

I'm trying to use sed in AppleScript to clean up a text file that has multiple lines. 我试图在AppleScript中使用sed清理具有多行的文本文件。

Source file (test.txt) 源文件(test.txt)

Some random text

<html>
Lorem ipsum dolor sit amet rhoncus
</html>

Even more random text

Applescript 苹果脚本

do shell script "sed 's/.*\\(<html.*html>\\).*/\\1/' test.txt"

Desired output: 所需的输出:

<html>
Lorem ipsum dolor sit amet rhoncus
</html>

Received output is unchanged since sed is only looking line by line. 由于sed仅逐行查找,因此接收到的输出保持不变。 Is there a way to force sed to look at the whole file instead? 有没有办法强迫sed查看整个文件?

This might work for you (GNU sed): 这可能对您有用(GNU sed):

sed -n '/^<html>/,/^<\/html>/p' file

Use seds grep-like option -n to turn off automatic printing, then use a range and only print lines within the range. 使用类似seds grep的选项-n关闭自动打印,然后使用一个范围并仅打印该范围内的行。

Alternatively: 或者:

sed '/^<html>/,/<\/html>/!d' file

如果您对awk那么以下内容也可以为您提供帮助。

awk '/<html>/,/<\/html>/' Input_file

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

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