Search code examples
shelldash-shell

Delete unimportant text from file in dash


I have a file with too many lines.

Its constructed like:

Text
Text
Text

<--!Important Text begins here-->
important Text
Important Text
Important Text

<--!Important Text ends here -->

Unimportant Text
....

<--!Important Text begins here-->
important Text
Important Text
Important Text

<--!Important Text ends here -->

Unimportant Text
....<--!Important Text begins here-->
important Text
Important Text
Important Text

<--!Important Text ends here -->

Unimportant Text
....

and so on.

How can i take the important part and save it in a new file? I am using the dash terminal from Macintosh


Solution

  • If you wish to include the markers then you can do something like:

    awk '/<--!Important Text begins here-->/,/<--!Important Text ends here -->/' file
    

    If you wish to ignore the markers and just print the content between them, you can do:

    awk '
    /<--!Important Text begins here-->/{p=1; next}
    /<--!Important Text ends here -->/{p=0}
    p' file
    

    The first solution is a regex range. It tells awk to print everything between the range (inclusive). To ignore the markers, you just need to set and unset the flag.