Reputation: 3274
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: [email protected]
cn: james ryan
displayName: James Ryan
givenName: James
sn: Ryan
zimbraMailDeliveryAddress: [email protected]
....
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: [email protected]
cn: james ryan displayName: James Ryan givenName: James sn: Ryan zimbraMailDeliveryAddress: [email protected]
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
Upvotes: 2
Views: 108
Reputation: 15461
Another way with sed :
sed '/^$/!{H;d};/^$/{x;G;s/\n/ /g;s/^ //}' file
Upvotes: 0
Reputation: 58420
This might work for you (GNU sed):
sed ':a;N;/\n\s*$/!s/\s*\n/ /;ta;s/\n//p;d' file
Read two or more lines into the pattern space (PS) replacing zero or spaces followed by a newline with a space if the last line read is not an empty line. If the last line read is empty, remove it and print the lines in the PS and then delete the PS.
N.B. This also caters for the last empty line not being present.
If the format of the file is fixed as in the example text:
sed 'N;N;N;N;N;s/\s*\n/ /g;s/ $//' file
may be suffice.
Upvotes: 1
Reputation: 21965
My awk
solution would be:
awk 'BEGIN{RS="";FS="\n";}
{
for(i=1;i<=NF;i++)
printf "%s%s", $i, (i<NF?OFS:ORS)
}' file
Output
cn: Jack displayName: Jack Johnson givenName: Jack sn: johnson zimbraMailDeliveryAddress: [email protected]
cn: james ryan displayName: James Ryan givenName: James sn: Ryan zimbraMailDeliveryAddress: [email protected]
Upvotes: 0
Reputation: 785156
awk
can handle this easily with empty RS
:
awk -v RS= '{gsub(/\n/, " ")} 1' file
cn: Jack displayName: Jack Johnson givenName: Jack sn: johnson zimbraMailDeliveryAddress: [email protected]
cn: james ryan displayName: James Ryan givenName: James sn: Ryan zimbraMailDeliveryAddress: [email protected]
By using RS=
we are splitting input data on records when we get an empty line after zimbraMailDeliveryAddress
lines.
Upvotes: 1
Reputation: 203522
$ awk -v RS= '{$1=$1}1' file
cn: Jack displayName: Jack Johnson givenName: Jack sn: johnson zimbraMailDeliveryAddress: [email protected]
cn: james ryan displayName: James Ryan givenName: James sn: Ryan zimbraMailDeliveryAddress: [email protected]
Upvotes: 6