Home>

There are the following files. When I saw it with the head command.

2020/6/30 2020/6/18 7:03 2020/6/23 4:53
2020/6/29 2020/6/23 1:40 2020/6/23 4:52
2020/2/24 2020/5/20 3:50 2020/6/18 1:18
2019/6/25 2020/5/20 3:49 2020/6/17 5:56
2019/6/25 2020/5/20 3:49 2020/6/17 5:55
2019/4/14 2020/5/20 3:49 2020/6/17 5:49
2019/4/13 2020/5/20 3:49 2020/6/17 5:44
2019/5/29 2020/5/20 3:49 2020/6/17 5:42
2019/6/5 2020/5/20 3:49 2020/6/17 5:42

This file has billions of lines and is huge, trying to output only a part (about 10,000 lines)

echo `head gene_data.txt -n 10000`>~/tmp/gene_data.txt

I did it, but the output result is not CSV, and there are multiple spaces lined up in one, and the line feed code disappears.

2020/6/30 2020/6/18 7:03 2020/6/23 4:53 2020/6/29 2020/6/23 1:40 2020/6/23 4:52 2020/2/24 2020/5/20 3:50 2020/6/18 1:18 2019/6/25 2020/5/20 3:49 2020/6/17 5:56 2019/6/25 2020/5/20 3:49 2020/6/17 5:55 2019/4/14 2020/5/20 3:49 2020/6/17 5:49 2019/4/13 2020/5/20 3:49 2020/6/17 5:44 2019/5/29 2020/5/20 3:49 2020/6/17 5:42 2019/6/5 2020/5/20 3:49 2020/6/17 5:42


I don't know why this happens, but after a lot of research, even though it has the function of "extracting some lines of the file and displaying them on the screen", it "extracts some lines of the file". I can't find the feature "Keep line breaks and tabs and output as a separate file".

I don't know if it's a minor method, but if anyone knows it, I'd appreciate it if you could tell me how.

  • Answer # 1

    The result of command substitution is displayed by echo, and the intention to redirect it is unknown. Unless you want to leave all tabs and line breaks blank, it doesn't seem to be the case when you read the question.

    Directly redirect the output of the head command

    head gene_data.txt -n 10000>~/tmp/gene_data.txt

    In the story that can be done.

  • Answer # 2

    echo echoIs a command that outputs arguments separated by a single space.

    Example:

    echo A B
    Output: A B

    simply,head gene_data.txt -n 10000>~/tmp/gene_data.txtIs it okay?

    There are other reasons,echo echoIf you want to do it in, enclose it in quotation marks to save the delimiter.
    Example:

    echo "A B"
    Output: A B
    echo "` head gene_data.txt -n 10000` ">~/tmp/gene_data.txt