Your Question: How Do You Print the Nth Line in Unix?
Contents

How do I print the nth line in Linux?

Below are three great ways to get the nth line of a file in Linux.

  1. head / tail. Simply using the combination of the head and tail commands is probably the easiest approach. …
  2. sed. There are a couple of nice ways to do this with sed . …
  3. awk. awk has a built in variable NR that keeps track of file/stream row numbers.

How do you find the nth line in Unix?

5 Answers. Sure. If the file is long and you’re only interested in one line, use sed to print the desired line and quit, or tail to remove the previous lines and head to extract the first line of the result. (Note that tail -n +$n skips n-1 lines, i.e. its output starts with the nth line.)

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest