unix - head AND tail of file

前端 未结 20 1259
误落风尘
误落风尘 2020-12-07 11:05

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

i.e. if the file is 200 lines long, then view lines

20条回答
  •  鱼传尺愫
    2020-12-07 11:50

    Consumes stdin, but simple and works for 99% of use cases

    head_and_tail

    #!/usr/bin/env bash
    COUNT=${1:-10}
    IT=$(cat /dev/stdin)
    echo "$IT" | head -n$COUNT
    echo "..."
    echo "$IT" | tail -n$COUNT
    

    example

    $ seq 100 | head_and_tail 4
    1
    2
    3
    4
    ...
    97
    98
    99
    100
    

提交回复
热议问题