Linux unix - 文件的头部和尾部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8624669/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:48:10  来源:igfitidea点击:

unix - head AND tail of file

linuxbashshellunixscripting

提问by toop

Say you have a txt file, what is the command to view the top 10 lines and bottom 10 lines of file simultaneously?

假设你有一个txt文件,同时查看文件的前10行和后10行的命令是什么?

i.e. if the file is 200 lines long, then view lines 1-10 and 190-200 in one go.

即,如果文件长 200 行,则一次性查看第 1-10 行和第 190-200 行。

采纳答案by Aleksandra Zalcman

You can simply:

您可以简单地:

(head; tail) < file.txt

And if you need to uses pipes for some reason then like this:

如果您出于某种原因需要使用管道,则如下所示:

cat file.txt | (head; tail)

Note: will print duplicated lines if number of lines in file.txt is smaller than default lines of head + default lines of tail.

注意:如果 file.txt 中的行数小于默认的头部行数+默认的尾部行数,则会打印重复的行。

回答by Paul

First 10 lines of file.ext, then its last 10 lines:

file.ext 的前 10 行,然后是最后 10 行:

cat file.ext | head -10 && cat file.ext | tail -10

cat file.ext | head -10 && cat file.ext | tail -10

Last 10 lines of the file, then the first 10:

文件的最后 10 行,然后是前 10 行:

cat file.ext | tail -10 && cat file.ext | head -10

cat file.ext | tail -10 && cat file.ext | head -10

You can then pipe the output elsewhere too:

然后,您也可以将输出通过管道传输到其他地方:

(cat file.ext | head -10 && cat file.ext | tail -10 ) | your_program

(cat file.ext | head -10 && cat file.ext | tail -10 ) | your_program

回答by mah

head -10 file.txt; tail -10 file.txt

head -10 file.txt; tail -10 file.txt

Other than that, you'll need to write your own program / script.

除此之外,您需要编写自己的程序/脚本。

回答by kev

edis the standard text editor

ed是个 standard text editor

$ echo -e '1+10,$-10d\n%p' | ed -s file.txt

回答by S.R.I

Well, you can always chain them together. Like so, head fiename_foo && tail filename_foo. If that is not sufficient, you could write yourself a bash function in your .profile file or any login file that you use:

好吧,你总是可以将它们链接在一起。像这样, head fiename_foo && tail filename_foo。如果这还不够,您可以在 .profile 文件或您使用的任何登录文件中编写自己的 bash 函数:

head_and_tail() {
    head  && tail 
}

And, later invoke it from your shell prompt: head_and_tail filename_foo.

而且,后来在shell提示符下调用它:head_and_tail filename_foo

回答by Samus_

the problem here is that stream-oriented programs don't know the length of the file in advance (because there might not be one, if it's a real stream).

这里的问题是面向流的程序事先不知道文件的长度(因为可能没有一个,如果它是一个真正的流)。

tools like tailbuffer the last n lines seen and wait for the end of the stream, then print.

工具,例如tail缓冲看到的最后 n 行并等待流结束,然后打印。

if you want to do this in a single command (and have it work with any offset, and do not repeat lines if they overlap) you'll have to emulate this behaviour I mentioned.

如果您想在单个命令中执行此操作(并使其与任何偏移量一起使用,并且如果它们重叠则不要重复行),您将必须模拟我提到的这种行为。

try this awk:

试试这个 awk:

awk -v offset=10 '{ if (NR <= offset) print; else { a[NR] = 
headtail() { awk -v offset="" '{ if (NR <= offset) print; else { a[NR] = 
headtail 10 < file.txt
; delete a[NR-offset] } } END { for (i=NR-offset+1; i<=NR; i++) print a[i] }' ; }
; delete a[NR-offset] } } END { for (i=NR-offset+1; i<=NR; i++) print a[i] }' yourfile

回答by lik

Why not to use sedfor this task?

为什么不用sed于此任务?

sed -n -e 1,+9p -e 190,+9p textfile.txt

sed -n -e 1,+9p -e 190,+9p textfile.txt

回答by Camille Goudeseune

To handle pipes (streams) as well as files, add this to your .bashrc or .profile file:

要处理管道(流)和文件,请将其添加到 .bashrc 或 .profile 文件中:

a.out | headtail 10

Then you can not only

那么你不仅可以

( COMMAND | tee /dev/fd/3 | head ) 3> >( tail )

but also

但是也

( ( seq 1 100 | tee /dev/fd/2 | head 1>&3 ) 2>&1 | tail ) 3>&1
( ( seq 1 100 | tee /dev/stderr | head 1>&3 ) 2>&1 | tail ) 3>&1

(This still appends spurious blank lines when 10 exceeds the input's length, unlike plain old a.out | (head; tail). Thank you, previous answerers.)

(当 10 超过输入的长度时,这仍然会附加虚假的空行,这与普通的 old 不同a.out | (head; tail)。谢谢以前的回答者。)

Note: headtail 10, not headtail -10.

注意:headtail 10,不是headtail -10

回答by RantingNerd

For a pure stream (e.g. output from a command), you can use 'tee' to fork the stream and send one stream to head and one to tail. This requires using either the '>( list )' feature of bash (+ /dev/fd/N):

对于纯流(例如命令的输出),您可以使用“tee”来分叉流并将一个流发送到头部,一个流发送到尾部。这需要使用 bash (+ /dev/fd/N) 的 '>( list )' 功能:

COMMAND | perl -e 'my $size = 10; my @buf = (); while (<>) { print if $. <= $size; push(@buf, $_); if ( @buf > $size ) { shift(@buf); } } print "------\n"; print @buf;'

or using /dev/fd/N (or /dev/stderr) plus subshells with complicated redirection:

或者使用/dev/fd/N(或/dev/stderr)加上带有复杂重定向的子shell:

##代码##

(Neither of these will work in csh or tcsh.)

(这些都不适用于 csh 或 tcsh。)

For something with a little better control, you can use this perl command:

对于一些更好的控制,你可以使用这个 perl 命令:

##代码##

回答by Gary van der Merwe

I wrote a simple python app to do this: https://gist.github.com/garyvdm/9970522

我写了一个简单的 python 应用程序来做到这一点:https: //gist.github.com/garyvdm/9970522

It handles pipes (streams) as well as files.

它处理管道(流)和文件。