13

On my Mac, when I try to run:

sed -i 2d file.csv

from the answer to Unix script to remove the first line of a CSV file, I get the following error:

sed: 1: "file.csv": invalid command code f

What can I do if I would like to delete the first two lines of file.csv?

Community
  • 1
  • 1
NumenorForLife
  • 1,736
  • 8
  • 27
  • 55

2 Answers2

24

On a Mac, the BSD sed requires the suffix for the backup (but the suffix can be an empty string, '') — it is not optional as it is with GNU sed. Hence, your command is being interpreted as "backup the file with the suffix 2d and … oops, the script you gave is file.csv, but f isn't a sed command".

sed -i .bak -e 2d file.csv

This deletes the first line of data from the CSV file (leaving the heading line in place).

If you want to write the code so it works with both BSD and GNU sed, then you have to attach the suffix to the -i option (GNU sed requires the suffix attached to the -i option; that's how it identifies whether there's an optional suffix or not):

sed -i.bak -e 2d file.csv

Note that you can't use an empty suffix and have the command work with both BSD sed and GNU sed.

The -e isn't necessary in either command line, but I quite often use it. I also often quote the command in single quotes, though it isn't necessary here.

If you want to delete the first two data lines, use 2,3d as the command. If you want to delete the first two lines, use 1,2d.

If you don't want the backup, then you can either remove it after the sed command completes (easiest) or use the two stage or three stage dance:

sed 2d file.csv > file.csv.bak &&
mv file.csv.bak file.csv            # Oops; there went the links

sed 2d file.csv > file.csv.bak &&
cp file.csv.bak file.csv
rm -f file.csv.bak

With these, you might need to add trap commands to clean up the intermediate .bak file if an interrupt or other signal terminates the script.


To quote from the Apple documentation for sed — which was originally quoted by Diego in an answer which he chose to delete, the -i option takes an argument which indicates the extension to use for backup copies.

-i extension

Edit files in-place, saving backups with the specified extension. If a zero-length extension is given, no backup will be saved. It is not recommended to give a zero-length extension when in-place editing files, as you risk corruption or partial content in situations where disk space is exhausted, etc.

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • This deletes the item in the second row, but not the first row: " sed 2d file.csv > file.csv.bak && cp file.csv.bak file.csv rm -f file.csv.bak" – NumenorForLife Apr 07 '15 at 03:15
  • 1
    I covered how to delete the first two lines in the file, and how to delete the first two data lines in the file. What more do you want? – Jonathan Leffler Apr 07 '15 at 03:18
3

sed -i.bak '2,3d' filename.csv will delete lines 1 and 2 (assuming you have headers)

What this command does is edits the file being referenced while simultaneously creating a backup of the original file (hence the usage of .bak). The line deletion '2,3d' will delete the 2nd and 3rd line of the original file.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278