user1502952
user1502952

Reputation: 1420

Sed multiline regular expression issue

File content:

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

Output:

Replaced
Success
opq rst
uvw xyz

Works as expected.

However for file content:

2013-09-17-01:02:43 User: [email protected]
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

This doesn't work.

Expected Output:

Replaced
Success
hello
how are you
catch up tmrw

Where is the issue?

Upvotes: 1

Views: 697

Answers (2)

konsolebox
konsolebox

Reputation: 75478

Make sure your file ends with LF and and not CRLF. Try to run it with dos2unix before running it with sed.

dos2unix file

Or completely strip off its trailing spaces (includes \r)

sed -i 's/[[:space:]]*$//' file

Upvotes: 1

iamauser
iamauser

Reputation: 11469

The way you have written does work. To make it more readable, this works too.

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

Upvotes: 1

Related Questions