Sed / Awk - remove spaces / line joins in ldif dump

I have some entries in my ldif file, which makes my dump bad for the next import.

sambaPasswordHistory: 712BC301C488FD2651BEF5AA11899950547B9ED3C059FF83CE39049B BAEECB31692629A94A3C1F4737E3EA854C001704793DB9A67EB977563CE601DF98E7E23C2851F 082D3D695C8655378629DCCDAF125ACA63141B361190ABC750AF403FDEF000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000 homeDirectory: /home_nfs/ 

How can I do using sed / awk / etc to change it to

 sambaPasswordHistory: homeDirectory: /home_nfs/ 

AKA save everything in one line

+4
source share
6 answers

One way: GNU sed :

 sed -n 'H; ${ x; s/\n//; s/\n //g; p}' file.txt 

Result:

 sambaPasswordHistory: homeDirectory: /home_nfs/ 
+8
source
 $ cat file sambaPasswordHistory: abc def 12345 67 homeDirectory: /home_nfs/ $ $ awk 'NR>1 && !sub(/^ /,""){print s; s=""} {s = s $0} END{print s}' file sambaPasswordHistory: abcdef1234567 homeDirectory: /home_nfs/ 
+2
source

One way to do it with sed:

 sed ':a;$!N;s/\n //;ta' file 

sed concatenates (N) each line other than the last line ($!). After joining, a newline, followed by a space (\ n), is deleted. "ta" is a loop to branch "a" until the change is complete.

+1
source

If the only occurrences of \n , that is, a new line followed by a space, are where the lines need to be combined, you can use bbe :

 <file bbe -e 's/\n //' 
0
source

This may work for you (GNU sed):

 sed '$!N;s/\(.*\)\n /\n\1/;T;D' file 

Indeed, the exercise in it does not use a cycle marker.

NB The guru's answer is much neat

0
source

Another solution:

 awk 'ORS="";!/home/{$1=$1; print}{RS="\n"}END{print "\n" $0 "\n"}' file 
0
source

Source: https://habr.com/ru/post/1442973/


All Articles