Issue
I am working on sed script that merges adjacent lines of text ending with the - character, if there are no white characters (space, tab) before it, together with the next line.
I have a script that doesn exactly that, however it doesn't filter properly.
My script
#!/bin/bash
file=$1
sed ':a;/-$/{N;s/-\n//;ba}' $file
This is my file input
line1-
line2
line3-
line4
line5 -
line6
And this is the result I want to see
line1-
line2
line3line4
line5 -
line6
And this is what I get
line1-
line2
line3line4
line5 line6
Solution
You might use a pattern to capture a non whitespace char in a group, and use that group in the replacement without using a label:
sed -E '/-$/{N;s/([^[:space:]])-\n/\1/}' $file
Output
line1-
line2
line3line4
line5 -
line6
See a sed demo.
Answered By - The fourth bird Answer Checked By - Cary Denson (WPSolving Admin)