linux - 删除文件中每个换行符之间的换行符

标签 linux bash awk sed

下面是文件的一部分,其中列出了 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

最佳答案

$ 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

关于linux - 删除文件中每个换行符之间的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38289613/

相关文章:

linux - 如何理解 pid() 和 new_pid 在执行 forktracker.stp 时是相同的值?

android - 使用sed操作xml文件

regex - 如何在 awk 命令中支持不常见的字符

linux - 使用 bash 脚本添加到 $PYTHONPATH

awk - 原始 awk 源代码的旧版本存档?

linux - 以编程方式启动交互式 shell

linux - 保存时文件权限发生变化(使用 root )

regex - 如何在 Bash 中测试变量是否符合指定格式?

linux - AWK 如何修改此代码以打印 num 个实例而不是 0 和 1

python - 哪个 linux 工具最适契约(Contract)时解析多个文件