Sed多线正则表达式问题

时间:2013-09-17 16:22:21

标签: bash shell sed

文件内容:

abc def
ijk lmn
opq rst
uvw xyz
sed '/\(.*\)def/{N;s/\(.*\)def\n\(.*\)lmn/Replaced\nSuccess/}' file

输出:

Replaced
Success
opq rst
uvw xyz

按预期工作。

但是对于文件内容:

2013-09-17-01:02:43 User: ID_123456@some.tld
2013-09-17-01:02:43 Last login time: Never
hello
how are you
catch up tmrw
sed '/\(.*\)tld/{N;s/\(.*\)tld\n\(.*\)Never/Replaced\nSuccess/}' file

这不起作用。

预期产出:

Replaced
Success
hello
how are you
catch up tmrw

问题出在哪里?

2 个答案:

答案 0 :(得分:1)

你写的方式确实有效。为了使其更具可读性,这也适用。

sed '/.*tld/{N;s/.*tld\n.*Never/Replaced\nSuccess/}' file

答案 1 :(得分:1)

确保您的文件以LF结尾,而不是CRLF。尝试使用dos2unix运行它,然后再使用sed。

运行它
dos2unix file

或完全剥离其尾随空格(包括\r

sed -i 's/[[:space:]]*$//' file
相关问题