satch_boogie satch_boogie - 5 months ago 15
Linux Question

remove newlines in file between every newlines

below is the part of file which has zimbra accounts listed(500+) separated by an empty newline

cn: Jack
displayName: Jack Johnson
givenName: Jack
sn: johnson
zimbraMailDeliveryAddress: Jack@example.com

cn: james ryan
displayName: James Ryan
givenName: James
sn: Ryan
zimbraMailDeliveryAddress: James@example.com

....


I want to have the file with the content like below so that i can import them to new server using
zmprove


cn: Jack displayName: Jack Johnson givenName: Jack sn: johnson zimbraMailDeliveryAddress: Jack@example.com
cn: james ryan displayName: James Ryan givenName: James sn: Ryan zimbraMailDeliveryAddress: James@example.com


i tried writing the script without removing new lines but couldnt extract so far

for line in `cat /tmp/account3.txt`;
do
echo $line | grep "zimbraMailDeliveryAddress:" > /dev/null
RESULT=$?

if [ $RESULT -eq 0 ]; then
email=`echo $line | cut -d' ' -f2` > /dev/null
continue

elif echo $line | grep "sn:" > /dev/null
RESULT=$?
if [ $RESULT -eq 0 ]; then
sn=`echo $line | awk '{ print $2; }'` > /dev/null
continue

elif echo $line | grep "givenName:" > /dev/null
RESULT=$?
if [ $RESULT -eq 0 ]; then
givenName=`echo $line | awk '{ print $2; }'` > /dev/null
continue

elif echo $line | grep "displayName:" > /dev/null
RESULT=$?
if [ $RESULT -eq 0 ]; then
displayName=`echo $line | awk '{ print $2; }'` > /dev/null
continue

elif echo $line | grep "cn:" > /dev/null
RESULT=$?
if [ $RESULT -eq 0 ]; then
cn=`echo $line | cut -d' ' -f2` > /dev/null
continue
fi
else
:
fi
echo $email $sn $cn $displayName $givenName
done
# awk '/cn:|displayName:|givenName:|sn:|zimbraMailDeliveryAddress:/{printf "%s ", $0; next} 1' /tmp/account2.txt

Answer
$ awk -v RS= '{$1=$1}1' file
cn: Jack displayName: Jack Johnson givenName: Jack sn: johnson zimbraMailDeliveryAddress: Jack@example.com
cn: james ryan displayName: James Ryan givenName: James sn: Ryan zimbraMailDeliveryAddress: James@example.com