如何从命令行将每两行合并为一行? [英] How to merge every two lines into one from the command line?

查看:485
本文介绍了如何从命令行将每两行合并为一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,格式如下。第一行是KEY,第二行是VALUE。

  KEY 4048:1736 string 
3
KEY 0:1772 string
1
KEY 4192:1349 string
1
KEY 7329:2407 string
2
KEY 0:1774 string
1

我需要与密钥在同一行中的值。所以输出应该看起来像这样...

$ p $ lt; code> KEY 4048:1736 string 3
KEY 0:1772 string 1
KEY 4192:1349 string 1
KEY 7329:2407 string 2
KEY 0:1774 string 1

如果我可以使用 $ 这样的分隔符会更好:

  KEY 4048:1736 string,3 

如何将两行合并为一行?

解决方案

awk:

  awk'NR%2 {printf%s,$ 0; next;} 1'yourFile 



请注意,输出结尾有空行。



sed:

  sed'N; s / \ n / /'yourFile 


I have a text file with the following format. The first line is the "KEY" and the second line is the "VALUE".

KEY 4048:1736 string
3
KEY 0:1772 string
1
KEY 4192:1349 string
1
KEY 7329:2407 string
2
KEY 0:1774 string
1

I need the value in the same line as of the key. So the output should look like this...

KEY 4048:1736 string 3
KEY 0:1772 string 1
KEY 4192:1349 string 1
KEY 7329:2407 string 2
KEY 0:1774 string 1

It will be better if I could use some delimiter like $ or ,:

KEY 4048:1736 string , 3

How do I merge two lines into one?

解决方案

awk:

awk 'NR%2{printf "%s ",$0;next;}1' yourFile

note, there is an empty line at the end of output.

sed:

sed 'N;s/\n/ /' yourFile

这篇关于如何从命令行将每两行合并为一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆