Unix命令将多行合并为一行

时间:2019-02-27 21:33:39

标签: unix

我有一个文件test.txt

outputa
outputb
outputc
outputa
outputb
outputc 

我需要这样的输出:

outputaoutputboutputc 
outputaoututboutputc 

在每次出现outputa时,都应断开行。

1 个答案:

答案 0 :(得分:-1)

可能不完全是您想要的,但这应该可以帮助您入门:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class FileTransformer {

    public static void main(String args[]) throws IOException {
        String s1 = readFile("test.txt");
        s1 = transform(s1, args[0]);
        writeFile(s1);
    }

    private static String readFile(String fileName) throws IOException {
        return new String(Files.readAllBytes(Paths.get(fileName)));
    }

    private static String transform(String input, String elementToStartNewLine) {
        return input.replace("\n", "").replace(elementToStartNewLine, "\n" + elementToStartNewLine);
    }

    private static void writeFile(String content) throws IOException {
        Files.write(Paths.get("output.txt"), content.getBytes());
    }
}

用法:java FileTransformer输出a