简体   繁体   English

如何使用shell脚本将多行以模式开头的grep转换为单行

[英]how to grep multiple line start with a pattern into a single line using shell script

What I'm trying to do is I would like to add multiple lines start with a pattern into a single line for example 我想做的是例如将以模式开头的多行添加到单行中

test.txt contains test.txt包含

name: test1
role: manager
mail: test1@mail.com
name:test2
role: analyst
name:test3

I would like to grep start from "name" and end at "name" but not all "name" will end with "mail" some is without "mail" 我想grep从“名称”开始并以“名称”结束,但并非所有的“名称”都将以“邮件”结尾,有些则没有“邮件”

desired output: 所需的输出:

name: test1 role: manager mail: test1@mail.com
name:test2 role: analyst
name:test3 

Right tool for this is awk . 正确的工具是awk I am not sure this could be done with grep 我不确定这可以用grep完成

awk '{printf (/^name/?RS:FS)"%s",$0}' test.txt

name: test1 role: manager mail: test1@mail.com
name:test2 role: analyst
name:test3

A gnu awk version (due to multiple characters in Record Separator) 一个gnu awk版本(由于Record Separator中有多个字符)

awk 'NF{$1=$1;print RS,$0}' RS="name:" test.txt
name: test1 role: manager mail: test1@mail.com
name: test2 role: analyst
name: test3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM