是文件的一部分,其中列出了以空白换行符分隔的zimbra帐户(500+)
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
....
我想让文件包含如下内容,以便我可以使用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
我尝试编写脚本而不删除新行,但到目前为止无法提取
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
答案 0 :(得分:6)
$ 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
答案 1 :(得分:1)
awk
可以使用空RS
:
awk -v RS= '{gsub(/\n/, " ")} 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
使用RS=
时,当我们在zimbraMailDeliveryAddress
行之后得到一个空行时,我们会在记录上拆分输入数据。
答案 2 :(得分:1)
这可能适合你(GNU sed):
sed ':a;N;/\n\s*$/!s/\s*\n/ /;ta;s/\n//p;d' file
如果读取的最后一行不是空行,则在模式空间(PS)中读取两行或多行替换零或空格,后跟带空格的换行符。如果读取的最后一行为空,请将其删除并打印PS中的行,然后删除PS。
N.B。这也适用于不存在的最后一条空行。
如果文件的格式是固定的,如示例文本中所示:
sed 'N;N;N;N;N;s/\s*\n/ /g;s/ $//' file
可能就够了。
答案 3 :(得分:0)
我的awk
解决方案是:
awk 'BEGIN{RS="";FS="\n";}
{
for(i=1;i<=NF;i++)
printf "%s%s", $i, (i<NF?OFS:ORS)
}' 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
答案 4 :(得分:0)
sed的另一种方式:
sed '/^$/!{H;d};/^$/{x;G;s/\n/ /g;s/^ //}' file