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 的缩写,正如英文单词 ...
随机推荐
- ps 证件照(1,2寸)
制作证件照 9*9打印 1,1寸 图片裁剪 2, 2寸 图片裁剪 3,将裁剪完成后的图片选择添加画布 Alt Ctrl c 将高和宽各加20px ,背景选择白色 4,将得到的带有白色 ...
- Oracle10g 连接 sqlserver hsodbc dblink 方式 非透明网关
Oracle10g 连接 sqlserver hsodbc dblink 方式 非透明网关 那个上传图片太麻烦了,发布到百度文库了 http://wenku.baidu.com/view/b38ae8 ...
- 【impala学习之二】impala 使用
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 CM5.4 一.Impala shell 1.进入impal ...
- 联想拯救者win10+ubuntu14.04
之前是win10+win7,默认win10启动,win7所在磁盘直接格式化即可(如果是win10+unbuntu不能这么搞,据说会导致win10也起不来) 按F2进bios 除了按这里fast boo ...
- AtCoder Beginner Contest 044 B - 美しい文字列 / Beautiful Strings
Time limit : 2sec / Memory limit : 256MB Score : 200 points Problem Statement Let w be a string cons ...
- Codeforce 814A - An abandoned sentiment from past (贪心)
A few years ago, Hitagi encountered a giant crab, who stole the whole of her body weight. Ever since ...
- Porsche Piwis Tester II Diagnostic Tool -Next Generation of PIWIS Tester KTS520
Porsche Piwis Tester II is the latest inspect equipment of Porsche Company. This is the latest profe ...
- APIClound 弹出层 Frame
JS api.openFrame({ name: 'showPic', url: './showPic.html', rect: { // x: api.pageParam.marginBottom, ...
- 细数那些不懂Spring底层原理带来的伤与痛
1. 什么是spring? Spring 是个Java企业级应用的开源开发框架.Spring主要用来开发Java应用,但是有些扩展是针对构建J2EE平台的web应用.Spring 框架目标是简化Jav ...
- EditPlus5.0注册码
EditPlus5.0注册码 注册名 Vovan 注册码 3AG46-JJ48E-CEACC-8E6EW-ECUAW EditPlus3.x注册码 EditPlus注册码生成器链接 http://ww ...