I have quite a few files where I want to get rid of a particular line break. The line always begins with "Title:" and so I tried the following command:
find . -type f -exec sed -i 's/^Title: \n\t*/Title:/g' {} \;
But from what I understand, sed
only handles one line at a time, and so the above won't work. Is there a way to remove a line break (in multiple files), but only if the line begins with some string?
With GNU sed
, you may use
sed '/^Title: $/{N;s/.*\n\t*\(.*\)/Title:\1/}'
See the online sed
demo
Details
/^Title: $/
- finds a line with contents equal to Title:
{N;s/.*\n\t*\(.*\)/Title:\1/}
- a block executed once the line above is found:
N
- gets the next line and appends a newline and that line to the pattern spaces/.*\n\t*\(.*\)/Title:\1/
- replaces the pattern space (that holds two lines now) using the .*\n\t*\(.*\)
pattern that matches
.*
- any 0+ chars\n\t*
- a newline and 0+ tabs\(.*\)
- Capturing group 1: any 0+ chars/Title:\1/
- replacement pattern, Title:
and the value inside Group 1.