Delete \n characters from line range in text file

后端 未结 4 1140
春和景丽
春和景丽 2021-01-29 05:07

Let\'s say we have a text file with 1000 lines.

How can we delete new line characters from line 20 to 500 (replace them with space for example)?

My try:

4条回答
  •  长情又很酷
    2021-01-29 05:26

    Here's a perl version:

    my $min = 5; my $max = 10;
    while () {
        if ($. > $min && $. < $max) {
            chomp;
            $_ .= " ";
        }
        print;
    }
    
    __DATA__
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    

    Output:

    1
    2
    3
    4
    5
    6 7 8 9 10
    11
    12
    13
    14
    15
    

    It reads in DATA (which you can set to being a filehandle or whatever your application requires), and checks the line number, $.. While the line number is between $min and $max, the line ending is chomped off and a space added to the end of the line; otherwise, the line is printed as-is.

提交回复
热议问题