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.