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 的缩写,正如英文单词 ...
随机推荐
- anconda1.8+cuda9.0+cudnn7.0.5+tensorflow1.7(win10)安装
1.下载安装cuda9.0 https://developer.nvidia.com/cuda-90-download-archive 2.下载cudnn7.0.5,下载cuda9.0的对应版本 ht ...
- 【函数封装】javascript判断是否是微信浏览器
//判断是否是微信浏览器的函数 function isWeiXin(){ //window.navigator.userAgent属性包含了浏览器类型.版本.操作系统类型.浏览器引擎类型等信息,这个属 ...
- python二叉树的深度遍历之先序遍历流程图
- Linux基础命令---文本显示tac
tac 将指定文件中的行,按照反序方式显示.此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. 1.语法 tac [选项] ...
- centos6.8安装DB2 10.5
1.把tar拷贝到/opt下面,用root账户,安装软件包 cd /opt tar -zxvf v9.5fp3b_linuxx64_server.tar.gz (64位) cd server ./db ...
- Qt 之 模态、非模态、半模态窗口的介绍及 实现QDialog的exec()方法
一.简述 先简单介绍一下模态与非模态对话框. 模态对话框 简单一点讲就是在弹出模态对话框时,除了该对话框整个应用程序窗口都无法接受用户响应,处于等待状态,直到模态对话框被关闭.这时一般需要点击对话框中 ...
- C#发送邮件异常:根据验证过程,远程证书无效,何解???
/// <summary> /// 发送邮件 /// </summary> /// <param name="mailSubjct">邮件主题& ...
- jQuery的ajaxFileUpload上传插件——刷新一次才能再次调用触发change
这个问题并不是由change事件失效造成的,而是ajaxFileUpload插件造成的,它会把原来的file元素替换成新的file元素,所以之前绑定的change事件就失效了. 查了一些资料,有些朋友 ...
- 使用json对象要注意的地方
本人是初学渣渣,使用json过程中遇到常见的问题,总结如下: 1--json对象内部不要有注释,虽然不报错,但是会使得渲染界面失败: 2--json对象是没有length属性的,若想获取到这个leng ...
- Django之Ajax刷新记住用户名
使用Cookie方法记住用户名 1.创建登录路由 2.定义登录视图函数 判断cookie中是否存在username,存在就将username渲染到login.html模板页面中 3.进行Ajax验证 ...