unix - head AND tail of file

前端 未结 20 1206
误落风尘
误落风尘 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:30

    Based on J.F. Sebastian's comment:

    cat file | { tee >(head >&3; cat >/dev/null) | tail; } 3>&1
    

    This way you can process first line and the rest differently in one pipe, which is useful for working with CSV data:

    { echo N; seq 3;} | { tee >(head -n1 | sed 's/$/*2/' >&3; cat >/dev/null) | tail -n+2 | awk '{print $1*2}'; } 3>&1
    
    N*2
    2
    4
    6
    

提交回复
热议问题