Question

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

Was it helpful?

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.

OTHER TIPS

Try the following:

sed -n '/<--!Important Text begins here-->/,/<--!Important Text ends here -->/ p' \
  infile | 
  fgrep -v -e '<--!Important Text begins here-->' \
           -e '<--!Important Text ends here -->' \
   > outfile

Note: Assumes that all <--!Important Text ... markers are on a separate line each.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top