Bash 工作代码从文件中删除第一行或最后一行
我看到了 sed 示例,但无论我怎么写,它都不会删除我的第一行。实际上,我做了更多测试,它也不会删除我的第一行,所以可以肯定我做错了什么:
sed '1d' filename
或者对于最后一行,
sed '$d' file name
我希望更改发生在同一个文件中,并且不希望有另一个输出文件。 那么,删除文件中最后一行的正确方法是什么?
I saw the sed
examples, but no matter how I write that it won't delete my first line. Actually, I did more tests and it won't delete my first line either, so for sure I'm doing something wrong:
sed '1d' filename
or for last line
sed '$d' file name
I want the changes to take place in the same file, and don't want another output file.
So please, what's the correct way to remove the last line in my file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
sed -i '$d' 文件名
。-i
标志就地编辑文件。sed -i '$ d' filename
. The-i
flag edits file in place.干得好 !
删除第一行(也兼容 BSD/MacOS)
删除最后一行/行
Here you go !
delete first line (also BSD/MacOS compatible)
delete last row/line
如果您的 sed 支持就地编辑,则为
sed -e '1d' -e '$d' -i filename
。If your sed supports in-place editing, it's
sed -e '1d' -e '$d' -i filename
.您可以使用
--in-place
(-i
) 开关:来源:man sed
You can use the
--in-place
(-i
) switch:Source: man sed
因为没有人给予它任何爱:
Because nobody gives it any love:
由于
sed
未标记,所以给出这个答案。Giving this answer since
sed
is not tagged.尝试cat file1 | sed "1,1d; $d" >文件2
Try
cat file1 | sed "1,1d; $d" > file2