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 的缩写,正如英文单词 ...
随机推荐
- Python全栈-库的操作
一.系统数据库 安装数据库系统后,系统自带的数据库.通过mysql客户端连接数据库系统后,使用show命令可查看系统中存在的所有库: mysql> show databases; +------ ...
- HDU1007.Quoit Design
-- 点我 -- 题目大意 :给你一堆点,求一个最小圆能够覆盖两个点的半径(最近两点距离的一半): 最多100000个点,暴力即O(n^2)会超时,考虑二分,先求左边最短距离dl,右边dr, 和一个点 ...
- python - 6. Defining Functions
From:http://interactivepython.org/courselib/static/pythonds/Introduction/DefiningFunctions.html Defi ...
- (cvpr2019 ) Technology details of Deep Learning for Multiple-Image Super-Resolution
Mutiple-Image SSR 关键的技术imformation fusion 1. 将单一场景的多图像经过Resnet, 其中每张图片的维度变为了输入的两倍.同时,这些输入的单一场景的多图像进行 ...
- Spring整合Shiro
apache shiro 是一个安全认证框架,和 spring security 相比,在于他使用了比较简洁易懂的 认证和授权方式.其提供的 native-session(即把用户认证后的授权信息保存 ...
- linux 系统监控和进程管理
1.命令top,查看cpu和内存使用,主要进程列表和占用资源. 2.内存使用命令foree -g 3.查询所有java进程:pgrep -l java ------ps aux|grep .j ...
- ajax 检测用户名是否可用
下面是一个 ajax 检测用户名是否可用的例子. django 项目中. —— views.py 里—— from django.shortcuts import render,HttpRespon ...
- Python进阶【第七篇】文件处理
一.文件操作 在Python中,文件读写是最常见的操作.对文件的操作为: #1. 打开文件,得到文件句柄并赋值给一个变量 f=open('a.txt','r',encoding='utf-8') #默 ...
- shell中test命令方法详解
test命令用法.功能:检查文件和比较值 1)判断表达式 if test (表达式为真) if test !表达式为假 test 表达式1 –a 表达式2 两个表达式 ...
- Eloquent JavaScript #09# Regular Expressions
索引 Notes js创建正则表达式的两种方式 js正则匹配方式(1) 字符集合 重复匹配 分组(子表达式) js正则匹配方式(2) The Date class 匹配整个字符串 Choice pat ...