Bash:删除除最后 n 行之外的所有输入行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19977273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash: remove all lines of input except last n lines
提问by rupat
i want to remove all lines except n last or first n lines with a short one-liner
我想删除除最后 n 行或前 n 行之外的所有行,并使用短单行
eg.:
例如。:
---
aaaa
bbbb
cccc
dddd
cat/echo/find ... | sed < all except last 2 lines >
should result
应该结果
aaaa
bbbb
---
aaaa
bbbb
cccc
dddd
eeee
ffff
cat/echo/find ... | sed < all except last 2 lines >
should result
应该结果
aaaa
bbbb
cccc
dddd
---
I need this also for very high n′s. So it could possible to set n=100 or so ;)
对于非常高的 n,我也需要这个。所以可以设置 n=100 左右;)
Thanx for any help !
感谢您的帮助!
回答by Etan Reisner
From the head man page:
从头部手册页:
-n, --lines=[-]N
print the first N lines instead of the first 10; with the lead-
ing ‘-', print all but the last N lines of each file
which means ... | head -n -2
will do what you want.
这意味着... | head -n -2
会做你想做的事。
Just as an alternative, the following also works, though I imagine its efficiency is not at all good for large files.
作为替代方案,以下内容也有效,但我认为它的效率对于大文件一点也不好。
tac file | awk 'NR>2' | tac
回答by Ivaylo Strandjev
To removethe last 2 lines you can use something like this:
要删除最后两行,您可以使用以下内容:
head -n $(($(wc -l < "$filename") - 2)) "$filename"
Not very elegant but it should work. I use wc -l to count the number of lines and then I use head to only display the first number of lines minus 2 lines.
不是很优雅,但它应该工作。我使用 wc -l 来计算行数,然后我使用 head 只显示第一个行数减去 2 行。
EDIT: to keeponly last N
lines you could do
编辑:只保留N
你可以做的最后几行
tail -n $N $filename
To keeponly first N
lines you could do.
为了保持只有第一N
,你可以做线。
head -n $N $filename
It seems I may have misunderstood your question and that is why I add these two commands.
看来我可能误解了您的问题,这就是我添加这两个命令的原因。
回答by Oleg S Kleshchook
i want to remove all lines except n last
我想删除除 n 最后的所有行
Easiest way is to do (let n=5):
最简单的方法是(让 n=5):
tail -n5 $filename > $filename.tmp && mv $filename.tmp $filename
This will output last five lines to new file and than rename it.
这会将最后五行输出到新文件,然后重命名它。
If the task is opposite - you need all lines, but 5 last, you can to do:
如果任务相反 - 您需要所有行,但最后 5 行,您可以执行以下操作:
head -n$((`cat $filename | wc -l` - 5)) $filename > $filename.tmp && mv $filename.tmp $filename
回答by Shehbaz Ahmed
To keep last 1000 lines of a file (Ex: alert_PROD.log)
保留文件的最后 1000 行(例如:alert_PROD.log)
tail -1000 alert_PROD.log > alert_tmp ; cat alert_tmp > alert_PROD.log ; rm alert_tmp
To keep top 1000 lines use head instead of tail command:
要保持前 1000 行,请使用 head 而不是 tail 命令:
head -1000 alert_PROD.log > alert_tmp ; cat alert_tmp > alert_PROD.log ; rm alert_tmp
回答by Ed Morton
To print all but the first 3 and then all but the last 3 lines of a file:
要打印文件的除前 3 行之外的所有行,然后打印除最后 3 行之外的所有行:
$ cat -n file
1 a
2 b
3 c
4 d
5 e
6 f
7 g
$ awk -v sz="$(wc -l < file)" 'NR>3' file
d
e
f
g
$ awk -v sz="$(wc -l < file)" 'NR<=(sz-3)' file
a
b
c
d
回答by Cole Tierney
To delete the last 3 lines of inputfile:
删除输入文件的最后 3 行:
set 3 inputfile; sed "$(($(sed -n $= )-+1)),$d"
回答by H?kon H?gland
You could also try
你也可以试试
awk -vn=2 -f rem.awk input.txt
where rem.awk
is
这里rem.awk
是
BEGIN { ("wc -l " ARGV[1]) | getline
a=-n+1 }
NR<a { print }
回答by sigma
You can use head and tail:
您可以使用头部和尾部:
To remove all lines except first n lines:
删除除前 n 行以外的所有行:
head -n filename > new_filename
head -n 文件名 > new_filename
To remove all lines except last n lines:
删除除最后 n 行以外的所有行:
tail -n filename > new_filename
tail -n 文件名 > 新文件名
Here is an example:
下面是一个例子:
$ cat test
$猫测试
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
$ head -2 test
$ head -2 测试
1
1
2
2