unix - head AND tail of file

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

    I would say that depending upon the size of the file, actively reading in its contents may not be desirable. In that circumstance, I think some simple shell scripting should suffice.

    Here's how I recently handled this for a number of very large CSV files that I was analyzing:

    $ for file in *.csv; do echo "### ${file}" && head ${file} && echo ... && tail ${file} && echo; done
    

    This prints out the first 10 lines and the last 10 lines of each file, while also printing out the filename and some ellipsis before and after.

    For a single large file, you could simply run the following for the same effect:

    $ head somefile.csv && echo ... && tail somefile.csv
    

提交回复
热议问题