• Linux/shell: Concatenate multiple lines to one line


    $ cat file
    START
    Unix
    Linux
    START
    Solaris
    Aix
    SCO

    1. Join the lines following the pattern START without any delimiter.
    $ awk '/START/{if (NR!=1)print "";next}{printf $0}END{print "";}' file
    UnixLinux
    SolarisAixSCO
    

    2. Join the lines following the pattern START with space as delimiter.
    $ awk '/START/{if (NR!=1)print "";next}{printf "%s ",$0}END{print "";}' file
    Unix Linux
    Solaris Aix SCO
    

    3. Join the lines following the pattern START with comma as delimiter.
    $ awk '/START/{if (x)print x;x="";next}{x=(!x)?$0:x","$0;}END{print x;}' file
    Unix,Linux
    Solaris,Aix,SCO
    
    4. Join the lines following the pattern START with comma as delimiter with also the pattern matching line.
    $ awk '/START/{if (x)print x;x="";}{x=(!x)?$0:x","$0;}END{print x;}' file
    START,Unix,Linux
    START,Solaris,Aix,SCO
    

    5. Join the lines following the pattern START with comma as delimiter with also the pattern matching line. However, the pattern line should not be joined.
    $ awk '/START/{if (x)print x;print;x="";next}{x=(!x)?$0:x","$0;}END{print x;}' file
    START
    Unix,Linux
    START
    Solaris,Aix,SCO

    6. Other ways:
    paste -d, -s file

    cat file | xargs


    REF:

    http://www.theunixschool.com/2012/05/awk-join-or-merge-lines-on-finding.html

    https://stackoverflow.com/questions/15758814/turning-multiple-lines-into-one-line-with-comma-separated-perl-sed-awk

    ==========================================================

    AC  M07451
    ID  V$PBX1_10
    NA  PBX1
    AC  M07452
    ID  V$MEIS1_06
    NA  Meis1
    AC  M07453
    ID  V$MEIS1BHOXA9PBX1A_01
    NA  Meis1b:Hoxa9:Pbx1a
    AC  M07454
    ID  V$GLI1_Q3_01
    NA  Gli1
    AC  M07455
    ID  V$GLI2_Q3
    NA  Gli2
    AC  M07456
    ID  V$HOXA913_Q4
    NA  HOX 9-13

    awk '$0 ~ /^AC|^ID|^NA/'  conserved.new

     |  awk '/AC  /{if (x)print x;x="";}{x=(!x)?$0:x","$0;}END{print x;}'

        | sed 's/AC  //g' | sed 's/,ID  / /g' | sed 's/,NA  / /g'

                  >  genesymbol


    M07451    V$PBX1_10    PBX1
    M07452    V$MEIS1_06    Meis1
    M07453    V$MEIS1BHOXA9PBX1A_01    Meis1b:Hoxa9:Pbx1a
    M07454    V$GLI1_Q3_01    Gli1
    M07455    V$GLI2_Q3    Gli2
    ==========================================================

    more file
    start111
    aa
    bb
    cc
    start222
    dd
    ee

    awk '/start/{if (x)print x;x="";}{x=(!x)?$0:x","$0;}END{print x;}' file > bbb

    more bbb
    start111,aa,bb,cc
    start222,dd,ee

    awk -F"[|, ]" '{for(i=2;i<=NF;i++){print $1"|"$i}}' bbb
    start111|aa
    start111|bb
    start111|cc
    start222|dd
    start222|ee

  • 相关阅读:
    linux查看CPU性能及工作状态的指令mpstat,vmstat,iostat,sar,top
    Linux vmstat命令实战详解
    dstat 性能监测工具
    sysstat 工具
    Linux命令详解----iostat
    Linux CPU实时监控mpstat命令详解
    Linux Top 命令解析 比较详细
    Linux统计/监控工具SAR详细介绍
    ubuntu 添加用户到已存在的组
    Ubuntu 14.04 使用速度极快的Genymotion 取代蜗牛速度的原生AVD模拟器
  • 原文地址:https://www.cnblogs.com/emanlee/p/7983668.html
Copyright © 2020-2023  润新知