How to print text between two line numbers into a new file in UNIX

12,842

Solution 1

use sed

sed -n '/^32/,/^39/p' file

Solution 2

Another way with sed:

 sed -n '32,39p' filename

Solution 3

I guess you should try this

sed '32,39!d' input > output

Solution 4

sed -n '32,39p' input > output

Note that this ignores your line numbering and simply prints lines 32 through 39. If you want to match the strings, use something like Fredrik's proposed solution.

Share:
12,842
Siva
Author by

Siva

Updated on June 05, 2022

Comments

  • Siva
    Siva almost 2 years

    I have a file like this (numbers on first column is line number)

    30.<unit id=20>
    31....
    32.</unit>
    33.<unit id=40?
    34....
    35.</unit>
    36<unit id=20>
    37...
    38.</unit>
    39.<unit id=40?
    40....
    41</unit>
    

    I want to print lines between two line numbers, say 32 and 39. How can I accomplish this in Unix?
    I'm new to unix. Note: I don't want perl scripts please.

  • Siva
    Siva about 11 years
    Thanks. There is small change now. Consider I don't have to manually input line numbers. I have a file like this: 13|20 35|45 75|96 546|633 I need a script for read this file for the line numbers and each of them in a new file. Thanks.
  • Siva
    Siva about 11 years
    Thanks. There is small change now. Consider I don't have to manually input line numbers. I have a file like this: 13|20 35|45 75|96 546|633 I need a script for read this file for the line numbers and each of them in a new file. Thanks.
  • Siva
    Siva about 11 years
    Thanks. There is small change now. Consider I don't have to manually input line numbers. I have a file like this: 13|20 35|45 75|96 546|633 I need a script for read this file for the line numbers and each of them in a new file. Thanks.
  • Fredrik Pihl
    Fredrik Pihl about 11 years
    @Siva - Upvote the answers to this question that you like. Mark one as the most appropriate answer (green check mark) and then post a new question
  • DontVoteMeDown
    DontVoteMeDown over 9 years
    Please add some explanation to your answer.
  • frazras
    frazras over 8 years
    This somehow fails the answer with the highest votes work