我需要使用bash脚本从一个巨大的文本文件中反复删除第一行。

现在我正在使用sed - I -e "1d" $FILE -但它需要大约一分钟的时间来删除。

有没有更有效的方法来实现这个目标?


当前回答

对于那些使用非gnu的SunOS的人来说,下面的代码将会有所帮助:

sed '1d' test.dat > tmp.dat 

其他回答

你可以很容易地做到这一点:

cat filename | sed 1d > filename_without_first_line

在命令行上;或者要永久删除文件的第一行,使用sed的in-place模式,并带-i标志:

sed -i 1d <filename>

对于那些使用非gnu的SunOS的人来说,下面的代码将会有所帮助:

sed '1d' test.dat > tmp.dat 

可以使用vim这样做:

vim -u NONE +'1d' +'wq!' /tmp/test.txt

这应该更快,因为vim在处理时不会读取整个文件。

可以使用sed命令按行号删除任意行

# create multi line txt file
echo """1. first
2. second
3. third""" > file.txt

删除行并打印到标准输出

$ sed '1d' file.txt 
2. second
3. third

$ sed '2d' file.txt 
1. first
3. third

$ sed '3d' file.txt 
1. first
2. second

# delete multi lines
$ sed '1,2d' file.txt 
3. third

# delete the last line
sed '$d' file.txt 
1. first
2. second

使用-i选项就地编辑文件

$ cat file.txt 
1. first
2. second
3. third

$ sed -i '1d' file.txt

$cat file.txt 
2. second
3. third

使用split怎么样?

man csplit
csplit -k file 1 '{1}'