Christian Bongiorno
Christian Bongiorno

Reputation: 5648

clever way to snips some lines from a file

I am trying to figure out how (without a temporary file at all) it's possible to cut, for example, lines 2-10 out of a file.

Basically, I need to remove entries in a CSV that have already been entered but keep the header.

I am sure someone out there is savvy enough for this

Upvotes: 0

Views: 53

Answers (1)

gniourf_gniourf
gniourf_gniourf

Reputation: 46833

You can use ed, the standard editor:

ed -s file.csv < <(printf '%s\n' '2,10d' 'wq')

The < <(printf ...) will drive ed to do the following:

  • Delete lines in the range 2–10 with the command 2,10d
  • Save (w) and exit (quit q) with wq.

Your version of ed may complain on wq; in this case give it 2 separate commands (w and then q) like so:

ed -s file.csv < <(printf '%s\n' '2,10d' 'w' 'q')

Otherwise, the obligatory sed way:

sed -i '2,10d' file.csv

which is just like the ed way, except that sed is not really a file editor: this uses (behind the curtains) a temporary file—so it doesn't technically fulfill your requirement.

Upvotes: 1

Related Questions