每行拆分多个字段以使用 sed 分隔行,保留行前缀 [英] Split multiple fields per line to separate lines using sed, retaining line prefix
问题描述
上周五我遇到了一个问题,将文本转换为另一种格式.在那台机器上,只有 gnu sed 可用,没有 awk(奇怪,我知道).我对 perl 一无所知.所以我正在寻找仅 sed 的解决方案.
Last Friday I got a problem, to transform a text to another format. On that machine, only gnu sed is available, no awk (strange, I know). And I know nothing about perl. so I am looking for a sed only solution.
文件内容为:
a yao.com sina.com
b kongu.com
c polm.com unee.net 21cn.com iop.com foo.com bar.com baz.net happy2all.com
d kinge.net
所需的输出,(应该是一个新文件)是:
the required output, (should be a new file) is:
a yao.com
a sina.com
b kongu.com
c polm.com
c unee.net
c 21cn.com
c iop.com
c foo.com
c bar.com
c baz.net
c happy2all.com
d kinge.net
我尝试了很多,也搜索了著名的 sed oneliner,但我无法成功......有人可以帮助我吗?
I tried a lot, also searched famous sed oneliner, but I cannot make it... can someone help me?
推荐答案
这对于 sed 来说并不容易,尤其是单行程序.但是你提到了gnu sed".我看到了曙光!
It is not easy job for sed, particularly, an one liner. however you mentioned "gnu sed". I see the light!
gnu sed 支持 s/.../.../ge
这对于这种情况很有用:
gnu sed supports s/.../.../ge
which is useful for this situation:
kent$ sed -r 's@(^[a-z]+) (.*)@echo "\2"\|sed "s# #\\n\1 #g"\|sed "/^$/d"@ge' file
a yao.com
a sina.com
b kongu.com
c polm.com
c unee.net
c 21cn.com
c iop.com
c foo.com
c bar.com
c baz.net
c happy2all.com
d kinge.net
简短说明:
- 外部 sed 是
sed -r 's@..x..@..y..@ge' 文件
ge
允许我们将匹配的部分传递给外部命令 ..y..
部分是由ge
的魔法完成的.我将\2
传递给另一个sed
(通过echo
):sed "s# #\\n\1 #g"
这个 sed 用\n + \1 + space
替换所有空格- 在原始文件中,每一行(结尾)都有
\n
,所以第2步(上面的步骤)的结果中有空行,我们需要删除那些空行"/^$/d"
- 最后,可以完成第 1 步中的替换(外部 sed),并得到结果.
- the outer sed is
sed -r 's@..x..@..y..@ge' file
thege
allows us pass matched part to external commands - The
..y..
part is done by the magic ofge
. I pass\2
to anothersed
(viaecho
) :sed "s# #\\n\1 #g"
this sed replace all space with\n + \1 + space
- in original file, there is
\n
on each line (ending), so there are empty lines in the result of step 2 (above step), we need remove those empty lines"/^$/d"
- finally, the substitution in step 1, (the outer sed), could be done, and we get the result.
检查 info sed
的 s/../../ge
编辑,添加双空格作为 OP 注释.
edit, added the double spaces as OP commented.
这篇关于每行拆分多个字段以使用 sed 分隔行,保留行前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!