I know this question has been asked and answered in different variations. But mine focuses on why sed is not behaving as I would expect vi does.
For a given threaddump file, I need to remove the newlines before every line that is " Locked ownable synchronizers" as shown below.
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
at com.project.tools.threads.NamedThread.run(NamedThread.java:37)
Locked ownable synchronizers:
- None
I can do this using vi:
:g/^M Locked ownable synchronizers/s// Locked ownable synchronizers/g
^^^ the ^M is ctrl-M. The above vi command works, ie, it successfully removes the newline before Locked. However, when I try to used it in sed, none of the following work (I tried multiple ways to represent the newline character but none worked).
sed -i'' -e 's/^M Locked ownable synchronizers/ Locked ownable synchronizers/g' file.threaddump
sed -i'' -e 's/\n Locked ownable synchronizers/ Locked ownable synchronizers/g' file.threaddump
sed -i'' -e 's/\r Locked ownable synchronizers/ Locked ownable synchronizers/g' file.threaddump
sed -i'' -e 's/\r\n Locked ownable synchronizers/ Locked ownable synchronizers/g' file.threaddump
As I understand, vi commands work in sed (and they have been). Why doesn't this one work????
Thank you
PS: The solution that worked was using perl:
perl -0pe 's/\n Locked ownable synchronizers:/ Locked ownable synchronizers:/g' < file.threaddump
but I want to figure out why the sed didn't work!