Chinaunix
标题:
文件太大 sed 不能处理怎么办?
[打印本页]
作者:
caozs
时间:
2015-10-14 12:04
标题:
文件太大 sed 不能处理怎么办?
我有一个大的XML文件,所有东东都在一行里面,有几百兆了。
想用sed 给他加上换行符, 可是大概因为单行太大,报错 couldn''t re-allocate memory
请问有没有有什么解决办法?
作者:
MMMIX
时间:
2015-10-14 13:05
回复
1#
caozs
那就换别的工具撒。
作者:
caozs
时间:
2015-10-14 13:13
用哪个工具呢?
回复
2#
MMMIX
作者:
MMMIX
时间:
2015-10-14 13:16
回复
3#
caozs
搜索都不会么?
作者:
caozs
时间:
2015-10-14 13:25
回复
4#
MMMIX
无语了,搜得到我就不来求助了,不知道答案的就别掺和了。
作者:
haooooaaa
时间:
2015-10-14 13:42
本帖最后由 haooooaaa 于 2015-10-14 14:24 编辑
无聊写了一个, 测试还行, read 里面的值是一次读入多少,
#!/usr/bin/env python
filename = "2.txt"
tmp = ""
with open(filename) as f:
while True:
tmp_read = f.read(11)
if tmp_read:
tmp += tmp_read
while "><" in tmp:
print tmp.split("><")[0] + ">"
tmp = "<" + "><".join(tmp.split("><")[1:])
else:
print tmp
break
复制代码
作者:
yjh777
时间:
2015-10-14 15:01
xmllint --format
作者:
yjh777
时间:
2015-10-14 15:04
xmllint 命令在 libxml2 包里面:
RHEL/CentOS/Fedora : yum install libxml2
debian/ubuntu: apt-get install libxml2-utils
作者:
jason680
时间:
2015-10-14 15:15
回复
1#
caozs
http://bbs.chinaunix.net/thread-4179098-1-1.html
作者:
yjh777
时间:
2015-10-14 15:25
xml 这种东西还是用专门的解析工具吧:
补充: xmllint --format 时, indent 可以通过环境变量 XMLLINT_INDENT 自定义
XMLLINT_INDENT=" " xmllint --format ur.xml
作者:
caozs
时间:
2015-10-14 15:29
回复
8#
yjh777
谢谢! 我已经写了个C 程序转换了, 但这个xmllint工具应该靠谱,下回试一试。
作者:
yjh777
时间:
2015-10-14 15:56
回复
11#
caozs
嗯,像 xml yaml json 这些东西早已有高效稳定的库/工具了,直接调用就好;
不需要自己重头写,不过有兴趣练练手也挺好的 : )
shell里提取xml结点的值 一般也可以用 xmllint --xpath:
xmllint --xpath "string($memberxPath/member[$idx]/value/string)" $xml
#btw: 最近用tcl的 tDOM 非常便利
set doc [dom parse $XML]
set root [$doc documentElement]
set recipeNodeList [$root selectNodes {//recipe}] #get all recipe node
foreach recipeNode $recipeNodeList {
set arch [$recipeNode @arch] # get attribute value
set type [$recipeNode @type]
#
}
作者:
MMMIX
时间:
2015-10-14 21:40
回复
5#
caozs
搜不到只能说明你太菜。
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2