简体   繁体   English

使用 Bash 将文本文件的最后 n 行移到顶部

[英]Move last n lines of a text file to the top with Bash

How can I move last n lines of a text file to the top without knowing number of lines of the file?如何在不知道文件行数的情况下将文本文件的最后 n 行移动到顶部? Is it possible to achieve this with single command line (with sed for example)?是否可以使用单个命令行(例如使用sed )来实现这一点?

From:从:

...
a
b
c
d

To:到:

a
b
c
d
...

Updated 2020: 2020 年更新:

You may find it hard if the input source is from pipe.如果输入源来自管道,您可能会发现很难。 In this case, you can use在这种情况下,您可以使用

awk '{a[NR-1]=$0}END{for(i=0;i<NR;++i)print(a[(i-4+NR)%NR])}'

This will store all lines in memory (which may be an issue) and then output them.这会将所有行存储在内存中(这可能是一个问题),然后输出它们。 Change 4 in the command to see different results.在命令中更改4以查看不同的结果。


Display the last n lines and then display the rest:显示最后 n 行,然后显示其余的行:

tail -n 4 file; head -n -4 file

From man head :man head

-n, --lines=[-]NUM -n, --lines=[-]NUM

print the first NUM lines instead of the first 10;打印前 NUM 行而不是前 10 行; with the leading '-', print all but the last NUM lines of each file使用前导“-”,打印每个文件的最后 NUM 行以外的所有行

tail -n 4 will display the last 4 lines of a file. tail -n 4将显示文件的最后 4 行。

If you wish to pipe this data, you need to put them together like this:如果你想通过管道传输这些数据,你需要像这样把它们放在一起:

( tail -n 4 file; head -n -4 file ) | wc

Or maybe you can use vim to edit file inline:或者您可以使用 vim 内联编辑文件:

vim +'$-3,$m0' +'wq' file

The + option for vim will run the (Ex) command following it. vim 的+选项将运行其后的 (Ex) 命令。 $-3,$m0 means move lines between 3 lines above the last line and the last line to the beginning of the file. $-3,$m0表示在最后一行和最后一行之间的 3 行之间移动行到文件的开头。 Note that there should be no space between + and the command.请注意, +和命令之间不应有空格。


Or using commands in vim normal mode:或者在 vim 正常模式下使用命令:

vim +'norm G3kdGggPZZ' file

G go to file end; G转到文件尾; 3k move up 3 lines; 3k向上移动 3 行; dG deletes to file end; dG删除到文件尾; gg go to file top; gg转到文件顶部; P pastes the deleted lines before this line; P将删除的行粘贴在该行之前; ZZ saves and quits. ZZ保存并退出。

This might work for you (GNU sed):这可能对你有用(GNU sed):

sed '$!H;1h;$!d;G' file

Append every line but the last to the hold space and then append the hold space to the the last line.将除最后一行之外的每一行附加到保留空间,然后将保留空间附加到最后一行。

It is trivial to do with eded无关

Move the last 30 lines on top of the file.将最后30行移到文件顶部。

printf '%s\n' '30,$m0' ,p Q | ed -s file.txt

Move 10 to 50 lines on top of the file.在文件顶部移动 10 到 50 行。

printf '%s\n' '10,50m0' ,p Q | ed -s file.txt

Move 1 to 10 to the last line/buffer.将 1 到 10 移动到最后一行/缓冲区。

printf '%s\n' '1,10m$' ,p Q | ed -s file.txt

Move 40 to 80 at the last line/buffer在最后一行/缓冲区移动 40 到 80

printf '%s\n' '40,80m$' ,p Q | ed -s file.txt
  • Line address 0 is the first and m means move行地址0是第一个, m表示移动

  • $ is the last line of the buffer/file. $是缓冲区/文件的最后一行。

  • Change Q to w to actually edit the file.txtQ更改为w以实际编辑file.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM