0

I have an enormous file, about 10 MB, and it has about 175,000 lines. I tried truncated it like this:

sed '500,175000d' <file-name.data>

I reopen the file, and all of the lines are still there! I tested this with other files and it works. For some reason the .data extension doesn't work? How do I delete these lines?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
makansij
  • 9,303
  • 37
  • 105
  • 183
  • weird that they don't mention that [here](http://www.folkstalk.com/2012/06/delete-range-of-lines-unix-linux.html) thanks – makansij Jul 09 '15 at 22:09
  • Actually you need `sed -i.bak ....` – makansij Jul 09 '15 at 22:35
  • That's unusual under Linux (which appears in the tags). BSD sed needs an extension argument to `-i` (or at least an empty string), GNU sed doesn't, and all Linux distributions I know ship GNU sed. Of course, it doesn't hurt to have a backup in case things go wrong. – Wintermute Jul 09 '15 at 22:47

1 Answers1

1

You need to either redirect the output to a new file like

sed '500,175000d' file-name.data >newFile

or use the edit in place option which rewrites the input file

sed -i '500,175000d' file-name.data

as pointed out by Wintermute

Edit:

A faster sed would be just

sed -i '500q' file-name.data # prints 1-500 and quits after line 500
Tom
  • 54
  • 4
  • The "faster" sed that you mention: If I do `sed -i '500q' file-name` then it gives me an error: `file-name extra characters at the end of command`. And, if I do `sed -i.bak '500q' file-name`, then it doesn't throw an error, but it doesn't change the file at all. My OS is Darwin Kernel version 14.3.0. – makansij Jul 11 '15 at 09:57
  • There are two implementations of sed(or more), look at this [post](http://stackoverflow.com/questions/4247068/sed-command-failing-on-mac-but-works-on-linux). Also there should be a space between -i and .bak – Tom Jul 11 '15 at 19:21