博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux/shell: Concatenate multiple lines to one line
阅读量:4878 次
发布时间:2019-06-11

本文共 2286 字,大约阅读时间需要 7 分钟。

 

$ cat fileSTARTUnixLinuxSTARTSolarisAixSCO 1. Join the lines following the pattern START without any delimiter.
$ awk '/START/{if (NR!=1)print "";next}{printf $0}END{print "";}' fileUnixLinuxSolarisAixSCO 2. Join the lines following the pattern START with space as delimiter.
$ awk '/START/{if (NR!=1)print "";next}{printf "%s ",$0}END{print "";}' fileUnix LinuxSolaris 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;}' fileUnix,LinuxSolaris,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;}' fileSTART,Unix,LinuxSTART,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;}' fileSTARTUnix,LinuxSTARTSolaris,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  /\t/g' | sed 's/,NA  /\t/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

 

转载于:https://www.cnblogs.com/emanlee/p/7983668.html

你可能感兴趣的文章
简单通用线程池的实现
查看>>
KMP算法详解
查看>>
2019.4.27
查看>>
Linux 操作
查看>>
【3】JAVA---地址App小软件(AddPanel.class)(表现层)
查看>>
tableau
查看>>
BZOJ 3195 [Jxoi2012]奇怪的道路 | 状压DP
查看>>
Java NIO框架Netty教程(二) 白话概念
查看>>
长序列处理
查看>>
vue打包后css背景图片地址找不到
查看>>
ThoughtWorks 一道面试题及解法
查看>>
系统之锹sysdig:Linux服务器监控和排障利器
查看>>
Java环境----JDK开发环境搭建及环境变量配置
查看>>
mysql init-file参数中语句限制
查看>>
scala 第一课
查看>>
$(selector).each() 和$each() 的区别
查看>>
【转】Objective-C Class Dump
查看>>
[转]Rails 3 | Bundler浅尝
查看>>
360,hold不住的流量吗?
查看>>
CocoaPods(第三方类库管理工具)
查看>>