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

sat*_*gie 2 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

....
Run Code Online (Sandbox Code Playgroud)

我想让文件包含如下内容,以便我可以使用它将它们导入新服务器 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
Run Code Online (Sandbox Code Playgroud)

我尝试编写脚本而不删除新行,但到目前为止无法提取

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
Run Code Online (Sandbox Code Playgroud)

Ed *_*ton 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
Run Code Online (Sandbox Code Playgroud)

  • 当然,`RS = <null>`告诉awk将每个空白行分隔的文本块作为记录读取,然后通过替换默认输入字段分隔符`FS`(任何白色空间链,包括换行符)重新编译记录)使用默认的输出字段分隔符`OFS`(单个空白字符)然后打印结果记录由真实条件"1"提供,导致该默认操作发生.我建议你阅读Arnold Robbins的书"Effective Awk Programming,4th Edition",如果你将在你的余生中做任何**文本操作:-). (3认同)