I want to get the second last line from the ls -l
output.
I know that
ls -l|tail -n 2| head -n 1
can do this, just wondering if sed
can do this in just one command?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
ls -l|sed -n 'x;$p'
It can't do third to last though, because sed only has 1 hold space, so can only remember one older line. And since it processes the lines one at a time, it does not know the line will be next to last when processing it. awk could return thrid to last, because you can have arbitrary number of variables there, but the script would be much longer than the tail -n X|head -n 1
.
回答2:
In a awk one-liner :
echo -e "aaa\nbbb\nccc\nddd" | awk '{v[c++]=$0}END{print v[c-2]}'
ccc
回答3:
Try this to delete second-last line in file
sed -e '$!{h;d;}' -e x filename
回答4:
tac filename | sed -n 2p
-- but involves a pipe, too