Printing the middle N lines of a file in Linux

While working on my “e” alias in my aforalias project, I want the show only the entries of zip files, that is, without the header and footer sections. The “e” alias also shows the entries sorted by name, unlike the behavior of “unzip -l“, which displays:

Archive:  gradle-1.12-all.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
<entries>
---------                     -------
102761855                     9503 files

Thus I wanted to print lines[3 .. lines.length – 3].

Lo and behold, there’s a way to do that with “head” and “tail”, as follows:

unzip -l $1 | tail -n +4 | head --lines=-2

tail -n +X” means “start printing at line X”, where 1 is the first line of the file (not 0).

And “head --lines=-X” means “print all but the last X lines”.

Not the most intuitive pair of commands in this context, but they can be quite helpful.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s