Sed 在文本文件中换行? [英] Sed to wrap lines in text file?

查看:48
本文介绍了Sed 在文本文件中换行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Sed 的新手.我有一堆 ASCII 文件,其中包含如下所示的数据:

I am a newbie with Sed. I have a bunch of ASCII files containing data that's laid out like so:

Test_Version:2.6.3
Model-Manufacturer:
HR21-100
Test_Version:2.6.3
Model-Manufacturer:
D12-100

我想要做的就是像这样折叠Model-Manufacturer:"的线条:

All I want to do is fold the lines for "Model-Manufacturer:" like this:

Test_Version:2.6.3
Model-Manufacturer:HR21-100
Test_Version:2.6.3
Model-Manufacturer:D12-100

我尝试使用 sed,因为我发现在Model-Manufacturer:"之后有 2 个十六进制值(0xA 代表 \n)

I tried to use sed since I discovered there are 2 hex values (0xA for \n) after "Model-Manufacturer:"

cat myfile| sed -n "/[[:xdigit:]]\{2\}/p"

我的匹配代码的输出如下所示:

The output looked like this for my match code:

Model-Manufacturer:
HR21-100
Model-Manufacturer:
D12-100

这告诉我我在正确的轨道上,对吗?我无法弄清楚的是折叠模型制造商:"行的正确匹配/替换代码.尝试使用

This tells me I am on the right track, right? What I cannot figure out is the proper match/replace code to fold the lines for "Model-Manufacturer:". A attempt with

cat myfile| sed "s/[[:xdigit:]]\{3\}//g"

完成搞砸了我的文件.知道如何做到这一点吗?

completed messed up my file. Any idea how to do this?

推荐答案

$ cat test.txt
Test_Version:2.6.3
Model-Manufacturer:
HR21-100
Test_Version:2.6.3
Model-Manufacturer:
D12-100

$ cat test.txt | sed -e '/:$/N; s/:\n/:/'
Test_Version:2.6.3
Model-Manufacturer:HR21-100
Test_Version:2.6.3
Model-Manufacturer:D12-100

这是每个 sed 命令的细分.

Here's a break down of each sed command.

/:$/N

如果输入行末尾有':',则在模式空间的末尾插入一个换行符,并追加输入的下一行.

If the input line has a ':' at the end, insert a newline at the end of the pattern space and append the next line of input.

s/:\n/:/

用:"替换后跟换行符的:",删除换行符.

Replace a ":" followed by a newline with ":", removing the newline.

这篇关于Sed 在文本文件中换行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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