Unix脚本,用于在Mac上删除文件的第一行 [英] Unix script to delete the first line of a file on a Mac
问题描述
在Mac上,当我尝试运行时:
On my Mac, when I try to run:
sed -i 2d file.csv
从 Unix脚本的答案中删除CSV文件,出现以下错误:
sed: 1: "file.csv": invalid command code f
如果我要删除file.csv
的前两行怎么办?
What can I do if I would like to delete the first two lines of file.csv
?
推荐答案
在Mac上,BSD sed
需要后缀进行备份(但后缀可以是空字符串''
),但不是与GNU sed
一样是可选的.因此,您的命令被解释为备份后缀为2d
和... oops的文件,给定的脚本为file.csv
,但f
不是sed
命令".
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
这将从CSV文件中删除第一行数据(保留标题行).
This deletes the first line of data from the CSV file (leaving the heading line in place).
如果要编写代码以便使其与BSD和GNU sed
都可使用,则必须将后缀附加到-i
选项(GNU sed
要求将后缀附加到-i
选项) ;它就是这样来确定是否有可选的后缀):
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
请注意,您不能使用空后缀,并且该命令可与BSD sed
和GNU sed
一起使用.
Note that you can't use an empty suffix and have the command work with both BSD sed
and GNU sed
.
在任一命令行中都不需要-e
,但是我经常使用它.我也经常用单引号将命令引起来,尽管这里没有必要.
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.
如果要删除前两个数据行,请使用2,3d
作为命令.如果要删除前两行,请使用1,2d
.
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
.
如果您不想备份,则可以在sed
命令完成后(最简单)将其删除(或者最简单),或者使用两阶段或三阶段舞蹈:
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
使用这些,如果中断或其他信号终止了脚本,则可能需要添加trap
命令来清理中间.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.
引用Apple文档中有关sed
的内容-最初由 Diego 在答案,-i
选项带有一个参数,该参数指示要用于备份副本的扩展名.
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
扩展名
就地编辑文件,以指定的扩展名保存备份.如果给出零长度的扩展名,将不保存任何备份.就地编辑文件时,不建议使用零长度的扩展名,因为在磁盘空间耗尽等情况下,您可能会损坏或部分内容.
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.
这篇关于Unix脚本,用于在Mac上删除文件的第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!