如何使用cat命令获取文件的最后一行
How to get the last line of a file using cat command
我正在 OSX(unix) 环境中编写 shell 脚本。我有一个名为 test.properties
的文件,其中包含以下内容:
cat test.properties
得到以下输出:
//This file is intended for
//blah blah purposes
123
使用 cat
命令,我怎样才能只获取文件的最后一行?
不要使用 cat
。 tail
正是针对此用例:
$ tail -1 ./test.properties
没有足够的声誉来评论 Mureinik 的 post 来回答你的最后一个问题,但如果你想在已知行之间显示文件的一部分,你可以尝试 sed -n '<line1>,<line2>p <filename>
。
如果您不知道这些行,请混合使用 tail
和 sed
:对于您的文件:
tail -r test.properties | sed -n '2p'
我正在 OSX(unix) 环境中编写 shell 脚本。我有一个名为 test.properties
的文件,其中包含以下内容:
cat test.properties
得到以下输出:
//This file is intended for
//blah blah purposes
123
使用 cat
命令,我怎样才能只获取文件的最后一行?
不要使用 cat
。 tail
正是针对此用例:
$ tail -1 ./test.properties
没有足够的声誉来评论 Mureinik 的 post 来回答你的最后一个问题,但如果你想在已知行之间显示文件的一部分,你可以尝试 sed -n '<line1>,<line2>p <filename>
。
如果您不知道这些行,请混合使用 tail
和 sed
:对于您的文件:
tail -r test.properties | sed -n '2p'