如何将我的5GB 1线性文件转换为基于模式的线条?

时间:2014-04-26 13:39:26

标签: unix

我有一个带有JSON数据的5GB 1个衬里文件,每行都从这个模式“{”创建“开始。我需要能够在我的Mac上使用Unix命令将1个衬里的怪物转换成尽可能多的行应得的。任何命令?

ASCII English text, with very long lines, with no line terminators

4 个答案:

答案 0 :(得分:0)

不清楚它是如何成为“单行”文件,但每行以"{"created"开头,但也许python -mjson.tool可以帮助您入门:

cat your_source_file.json | python -mjson.tool > nicely_formatted_file.json

通过``python -mjson.tool`管道原始JSON将干净地格式化JSON,使其更具人性化。更多信息here

答案 1 :(得分:0)

您可以使用PHP作为shell命令(如果安装了PHP),只需保存名为“myscript”的文本文件和相应的代码(我现在无法测试代码,但想法如下)

未经审查的代码

#!/usr/bin/php
<?php

    $REPLACE_STRING='{"created'; // anything you like


    // open input file with fopen() in read mode
    $inFp=fopen('big_in_file.txt', "r");

    // open output file with fopen() in write mode
    $outFp=fopen('big_out_file.txt', "w+");


// while not end of file
while (!feof($inFp)) {

    // read file chunks here with fread() in variable $chunk
    $chunk = fread($inFp, 8192);

    // do a $chunk=str_replace($REPLACE_STRING,"\r".$REPLACE_STRING; // to add returns
    // (or use \r\n for windows end of lines)

    $chunk=str_replace($REPLACE_STRING,"\r".$REPLACE_STRING,$chunk);

    // problem: if chunk contains half the string at the end
    // easily solved if $REPLACE_STRING is a one char like '{'
    // otherwise test for fist char { in the end of $chunk
    // remove final part and save it in a var for nest iteration


    // write $chunk to output file
    fwrite($outFp, $chunk);

// End while
}

?>

保存之后你必须使它成为可执行文件sudo chmod a + x ./myscript

然后在终端

中将其作为./myscript启动

此后,myscript文件是一个完整的unix命令

答案 2 :(得分:0)

OS X附带flex和bison,您可以使用它们为您的数据编写解析器。

答案 3 :(得分:0)

如果你有足够的内存,你可以使用TextWrangler应用程序(免费的BBEdit表兄弟)打开文件一次,并在整个文件上使用常规搜索/替换。在替换中使用\ r来添加返回。打开文件会很慢,如果内存不足,甚至可能会挂起,但最终它可能会起作用。没有脚本,没有命令,等等。我用大型SQL文件做了这个,有时它完成了这项工作。

你必须用你的前面用\ n或\ r或\ r \ n的相同字符串替换你的行开始字符串。

相关问题