Solved remove leading space and join previous line

July 8, 2011 at 04:26:36
Specs: linux
hello community!

i have a text file (it`s a ldif export), and i would like to grep out the dn. the line with the dn starts with "dn:". no problem so far to grep for this pattern.
but the ldif export makes a line break after 76 characters, and continues the next line with a "space" character.

since i need the full dn, i need a way to:
1) search for this lines which start with a leading "space"
2) remove this "space"
3) and finally join the line which had the space, with the previous line

i have tried several ways with sed, awk, tr, ... but since i´m quite new to this topics i dont get it solved. does anyone here have a hint for me, how to do this task?

thanks in advance,

See More: remove leading space and join previous line

July 8, 2011 at 11:41:51
✔ Best Answer
With this sed script, if the line starts with a blank space, eliminate the space and append it to the previous line:

sed -e :a -e '$!N;s/\n //;ta' -e 'P;D' datafile

It does NOT check if the previous line starts with dn.

This is a mod of a script from Eric Pement's sed one-liners:

Let me know if you have any questions.

Report •

July 8, 2011 at 14:20:16
wow, thank you nails.
i have no clue how, but it works perfectly for my needs.
i´ll try to understand later on.
and thanks for the link to erics page *bookmarked*
i´m sure it will be of use in future.
thanks again and best regards

Report •

July 8, 2011 at 21:03:30
You are welcome. Eric's one-liners are explained at this link:

Look at explanation #40 for the explanation of this particular script.

Report •

Related Solutions

March 30, 2012 at 19:44:45
This didn't work for me on Mac OS X (FreeBSD tools).
The following one liner did though:

sed -n -e :a -e '/^$/d;$!N;s/\n //;ta' -e p -e 's/^dn:/\ndn:/' example.ldif

It will break the BASE64 encoding of binary attributes though (eg jpegPhoto).

Report •

Ask Question