简体   繁体   English

从bash的STDOUT移除n行

[英]remove n lines from STDOUT on bash

Do you have any bash solution to remove N lines from stdout? 您是否有任何bash解决方案可从stdout中删除N行?

like a 'head' command, print all lines, only except last N 像“ head”命令一样,仅打印最后一行N

Simple solition on bash: bash上的简单隔离:

find ./test_dir/ | 查找./test_dir/ | sed '$d' | sed'$ d'| sed '$d' | sed'$ d'| sed '$d' | sed'$ d'| ... ...

but i need to copy sed command N times 但是我需要复制sed命令N次

Any better solution? 有更好的解决方案吗? except awk, python etc... 除了awk,python等...

Use head with a negative number. 使用带有负数的head In my example it will print all lines but last 3: 在我的示例中,它将打印所有行,但最后3行:

head -n -3 infile

if head -n -3 filename doesn't work on your system (like mine), you could also try the following approach (and maybe alias it or create a function in your .bashrc) 如果head -n -3文件名在您的系统上不起作用(例如我的),您也可以尝试以下方法(也可以使用别名或在.bashrc中创建函数)

head -`echo "$(wc -l filename)" | awk '{ print $1 - 3; }'` filename

Where filename and 3 above are your file and number of lines respectively. 其中文件名和上面的3分别是您的文件和行数。

The tail command can skip from the end of a file on Mac OS / BSD. tail命令可以从Mac OS / BSD上的文件末尾跳过。 tail accepts +/- prefix, which facilitates expression below, which will show 3 lines from the start tail接受+/-前缀,这有助于下面的表达,从头开始显示3行

tail -n +3 filename.ext

Or, to skip lines from the end of file, use - prefixed, instead. 或者,要从文件末尾跳过行,请改用-前缀。

tail -n -3 filenme.ext

Typically, the default for tail is the - prefix, thus counting from the end of the file. 通常, tail的默认值是-前缀,因此从文件末尾开始计数。 See a similar answer to a different question here: Print a file skipping first X lines in Bash 在这里看到另一个问题的类似答案: 打印文件,跳过Bash中的前X行

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

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