-1

I use sed -n START_LINE,FINISH_LINEp filename > newfile to extract a specified range of text from a text file, e.g. sed -n 45,50p datafile > newfile.

How can I use this to extract multiple portions of text which are always bordered by the same pattern of text? e.g. the text I want to extract always begins 39 lines down from Subject:????? and ends immediately before Start Date: ??/??/??.

0

While this doesn't answer your question per se, awk has an elegant solution.

$ awk '

    # Set start variable
    /Subject/ { start=NR + 39; }

    # Disable print condition below
    /Start Date/ { start=""; next; }

    # Print while line is greater than start
    NR > start && start

' someFile.txt
  • 1
    I'm not sure if the questioner is tied to sed, but I think awk might be better anyway. FWIW I think they want multiple blocks of line, so the /Start Date/ line should just stop printing, rather than exit. – Sparhawk Mar 3 '16 at 3:41
  • Good catch @Sparhawk. – cdosborn Mar 3 '16 at 3:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.