1

I have some apt files that have code in them like this:

---

some code

---

Sometimes there's a blank line following the opening ---, or before the closing ---. I want to compress these (get rid of the unwanted "inside" blank lines). I'm quite willing to do so manually, but I'd like to print out a list of where all those are. I'm actually looking for \n\n---\n\n, but in the following I'll just show my work on \n\n---.

I've tried many variations on the following with no success:

grep -E \n\n---\n\n file.apt

Escaped the backslashes, tried \r, single and double quotes, grep --, w/o -E, ^$, etc. The following works:

grep \n\n file.apt

It prints all the right lines. The following also works:

grep "\---" file.apt

It prints all of the lines that have 3 hyphens. However, the following prints nothing:

grep "\n\n\---" file.apt

If I try that pattern (\n\n---) in vi I find what I'm looking for. All the failed grep attempts print nothing. So how can I find "\n\n---"?

I'm on Mac OSX, terminal command line.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user766353
  • 528
  • 5
  • 23
  • Have you tired wrapping these queries in single qoutes so bash will interpret them literally? – Josh Jul 15 '11 at 18:35
  • Yeah, all combinations, even putting in a real return in the middle of the quoted regex. Same unwanted results. – user766353 Jul 15 '11 at 20:22

2 Answers2

1

It is my understanding that grep only matches a single line at a time. If you want to get rid of blank lines try running

grep ^$ -v file.apt

^$ will match every blank line and then the -v will print only lines that don't match.

stimms
  • 42,945
  • 30
  • 96
  • 149
  • I'm actually trying to get rid of only certain blank lines. I've tried one-line perl and sed, same result. My hunch is that I'm running into different flavors of regex and that I need one that just recognizes \n or \r as a return. I used TextWrangler's multi-file find with regex and it worked beautifully. One caveat to that solution: I had to open all the files I needed to change, as it would not open them itself. – user766353 Jul 15 '11 at 20:21
0

This is basically a duplicate of: How to find patterns across multiple lines using grep?

There are good answers there that should help you. I'd probably use the sed option for what you are doing.

Community
  • 1
  • 1
Mark
  • 1,058
  • 6
  • 13