[英]Bash: remove all lines of input except last n lines
i want to remove all lines except n last or first n lines with a short one-liner 我想删除除了最后一行或前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. 我也需要这个非常高的n。 So it could possible to set n=100 or so ;)
所以可以设置n = 100左右;)
Thanx for any help ! Thanx任何帮助!
To remove the 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 keep only last N
lines you could do 编辑:只保留你可以做的最后
N
行
tail -n $N $filename
To keep only 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. 我似乎误解了你的问题,这就是我添加这两个命令的原因。
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
这意味着
... | head -n -2
... | 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
i want to remove all lines except n last
我想删除除最后一行之外的所有行
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
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
要删除inputfile的最后3行:
set 3 inputfile; sed "$(($(sed -n $= $2)-$1+1)),\$d" $2
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
You can use head and tail: 你可以使用头部和尾部:
To remove all lines except first n lines: 要删除除前n行之外的所有行:
head -n filename > new_filename
head -n filename> new_filename
To remove all lines except last n lines: 删除除最后n行之外的所有行:
tail -n filename > new_filename
tail -n filename> new_filename
Here is an example: 这是一个例子:
$ cat test $ cat测试
1 1
2 2
3 3
4 4
5 五
6 6
7 7
8 8
$ head -2 test $ head -2测试
1 1
2 2
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=$1-n+1 }
NR<a { print }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.