简体   繁体   中英

how to extract middle ten lines of a file using linux commands when total number of lines in the file is unknown?

Suppose there is a text file with unknown number of lines and we need to extract ten lines in the middle of the file. How can we achieve the above using Linux commands?

· wc : count lines (first result)

· awk : takes the lines returned by wc, divides by 2 and adds 5

· tail : gets the last -n lines

· head : gets the first -n lines

tail -n `wc filename  | awk '{print (int($1/2)+5) }' `  filename | head -n 10

This is aprox, doesn't work if you need check errors

Use wc to count number of lines

wc -l filename

Then, use Head and Tail to extract lines.
let's assume file has 50 lines.

head -n 30 filename | tail -n +21

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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