如何将文本文件的每两行与 Bash 配对? [英] How do I pair every two lines of a text file with Bash?

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

问题描述

使用一个简单的 bash 脚本,我生成了一个包含多行的文本文件,如下所示:

With a simple bash script I generate a text file with many lines like this:

192.168.1.1
hostname1
192.168.1.2
hostname2
192.168.1.3
hostname3

现在我想重新格式化这个文件,使它看起来像这样:

Now I want to reformat this file so it looks like this:

192.168.1.1 hostname1
192.168.1.2 hostname2
192.168.1.3 hostname3

我将如何以这种方式重新格式化?也许sed?

How would I reformat it this way? Perhaps sed?

推荐答案

$ sed '$!N;s/
/ /' infile
192.168.1.1 hostname1
192.168.1.2 hostname2
192.168.1.3 hostname3

这篇关于如何将文本文件的每两行与 Bash 配对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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