LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 8048|回复: 17

如何用sed合并两行?

[复制链接]
发表于 2004-7-12 21:51:43 | 显示全部楼层 |阅读模式
cat test

hello
ok

结果:

hello ok

这样好象不行,怪怪的:
sed 's/$/ /' test
发表于 2004-7-12 22:02:14 | 显示全部楼层
还是用xargs来的爽 ;)

  1. xargs -n2 <file
复制代码
 楼主| 发表于 2004-7-12 22:14:45 | 显示全部楼层
This is a test,
hello
ok
the bottom.

期望的结果:

This is a test,
hello ok
the bottom.

如果是这样便不好用xargs了
发表于 2004-7-12 22:21:45 | 显示全部楼层
呵呵,至少要有规律吧~,我看还是贴出样本吧~~~, ;)
 楼主| 发表于 2004-7-12 22:26:34 | 显示全部楼层
我的样本有9.x MB,太大
它的规律是:
找到包含文字abcde的一行,并把这一行和下一行合并成一行;就这么简单
我稻谷了办天也没想出来
发表于 2004-7-12 22:56:06 | 显示全部楼层
没想出好的法子 :sorry
before:
/home/lee#cat file
This is a test,
abcde
hello
ok
abcde
the botto
abcde
m.
This is a test,
abcde
hello
ok
abcde
the botto
abcde
m.

[php]
cat file|while read i;do
        echo $i
        if echo $i|grep -A1 abcde >/dev/null;then
                echo $i|tr '\n' ' '
        fi
done|sed '/^abcde$/d'
[/php]
after:
This is a test,
abcde hello
ok
abcde the botto
abcde m.
This is a test,
abcde hello
ok
abcde the botto
abcde m.

相信有更好的方法
 楼主| 发表于 2004-7-12 22:59:07 | 显示全部楼层
:thank
发表于 2004-7-13 01:30:19 | 显示全部楼层
不用这么麻烦吧?

  1. sed -e '/abcde/{
  2. N
  3. s/\n//
  4. }
复制代码
发表于 2004-7-13 01:49:04 | 显示全部楼层
最初由 alphatan 发表
不用这么麻烦吧?

  1. sed -e '/abcde/{
  2. N
  3. s/\n//
  4. }
复制代码

you are Right~~~ :thank
sed  '/abcde/{N;s/\n//}' file
ps:楼上的签名出自~~~<菜根谭>吧~~ :thank
发表于 2004-7-13 11:54:13 | 显示全部楼层
awk的方法:

  1. cat file|awk '{if(/abcde/){printf $0" ";next} else{print}}'
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表