1

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

that other guy
  • 116,971
  • 11
  • 170
  • 194
user3352472
  • 99
  • 1
  • 7

2 Answers2

1

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.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • fgrep: -e: No such file or directory fgrep: : No such file or directory fgrep: : No such file or directory sed: RE error: illegal byte sequence – user3352472 Feb 26 '14 at 10:13
  • @user3352472: Tell me what you get when you run `fgrep --version` - while you're at it, als run `sed --version` and `awk --version`; I'm unclear what you mean by "dash terminal" - if by "dash" you mean the [dash shell](http://en.wikipedia.org/wiki/Debian_Almquist_shell), that would be surprising; that said, it's not the _shell_ that matters here, but what versions of the _command-line tools_ you're running. – mklement0 Feb 26 '14 at 16:58
1

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.

mklement0
  • 382,024
  • 64
  • 607
  • 775
jaypal singh
  • 74,723
  • 23
  • 102
  • 147