0

我有几个相当大的文本文件(每个 1 GB),每行都有一个生成的单词。我想在每个生成的单词之前附加一个字符串。是 Java、C#、C、C++ 还是 Ruby 并不重要。虽然我不能自己编程,但我可以编译和运行它。

例子:

文件.txt:

Aoos
ahsd
gAATa
sdFfg

输出:

appendAoos
appendahsd
appendgAATa
appendsdFfg

欢迎任何帮助!

4

2 回答 2

1

You can just use sed from the command line, e.g.

$ sed 's/^/append/' < old_file.txt > new_file.txt
于 2013-03-13T09:27:43.263 回答
1

根据您可用的工具,您可以使用sedawk甚至perl

sed 's/^/append/' inputFile >outputFile
awk '{print "append"$0}' inputFile >outputFile
perl -pne 's/^/append/' inputFile >outputFile

如果你真的想编写自己的程序,你可以在 C 中相对简单地进行过滤程序:

#include <stdio.h>
int main (void) {
    int ch, lastCh = '\n';
    while ((ch = getchar()) != EOF) {
        if (lastCh == '\n') printf ("append");
        putchar (ch);
        lastCh = ch;
    }
    return 0;
}

例如,只需将其编译为,myprog然后运行:

myprog <inputFile >outputFile
于 2013-03-13T09:31:18.513 回答