Going to a specific line number using Less in Unix
Asked Answered
M

6

487

I have a file that has around million lines. I need to go to line number 320123 to check the data. How do I do that?

Mare answered 21/12, 2011 at 8:4 Comment(2)
What about doing that with more? :-)Crispi
because: less is more, but more more than more is, so more is less less, so use more less if you want less more.Janettjanetta
B
672

With n being the line number:

  • ng: Jump to line number n. Default is the start of the file.
  • nG: Jump to line number n. Default is the end of the file.

So to go to line number 320123, you would type 320123g.

Copy-pasted straight from Wikipedia.

Brigham answered 21/12, 2011 at 8:8 Comment(5)
... and don't hit enter after g, or you will jump one further line.Janettjanetta
This nor the other answers work for the BusyBox version of less: less 9581553g -N file.txt less: can't open '9581553g': No such file or directory and also: less +G -N file.txt less: can't open '+G': No such file or directoryBeem
@Beem you should enter these commands being in less not in shell. First you open the file less file.txt and then enter your command 9581553gHallucination
That makes so much more sense. I was treating it like sed or awk where you could give specific line numbers as parameters. Thank you for clarifying.Beem
if its a large file, it may take some time to seek to the position....be patientConventionalize
P
283

To open at a specific line straight from the command line, use:

less +320123 filename

If you want to see the line numbers too:

less +320123 -N filename

You can also choose to display a specific line of the file at a specific line of the terminal, for when you need a few lines of context. For example, this will open the file with line 320123 on the 10th line of the terminal:

less +320123 -j 10 filename
Permenter answered 6/3, 2014 at 11:2 Comment(0)
C
68

You can use sed for this too -

sed -n '320123'p filename 

This will print line number 320123.

If you want a range then you can do -

sed -n '320123,320150'p filename 

If you want from a particular line to the very end then -

sed -n '320123,$'p filename 
Cool answered 21/12, 2011 at 8:32 Comment(0)
J
47

From within less (in Linux):

 g and the line number to go forward

 G and the line number to go backwards

Used alone, g and G will take you to the first and last line in a file respectively; used with a number they are both equivalent.

An example; you want to go to line 320123 of a file,

press 'g' and after the colon type in the number 320123

Additionally you can type '-N' inside less to activate / deactivate the line numbers. You can as a matter of fact pass any command line switches from inside the program, such as -j or -N.

NOTE: You can provide the line number in the command line to start less (less +number -N) which will be much faster than doing it from inside the program:

less +12345 -N /var/log/hugelogfile

This will open a file displaying the line numbers and starting at line 12345

Source: man 1 less and built-in help in less (less 418)

Jackanapes answered 28/3, 2014 at 12:31 Comment(1)
interestingly, google decided to take parts of your answer for their displayed answer when googling: "less go to line" (a good answer imo)Excursus
L
5

For editing this is possible in nano via +n from command line, e.g.,

nano +16 file.txt

To open file.txt to line 16.

Lurlinelusa answered 12/6, 2013 at 20:49 Comment(1)
adding -c to the nano command is also useful - doing so will cause nano to always show the current line number while editingConventionalize
P
0

Just to add my 2 cents, in long files it is quicker to move to a given percentage, e.g typing 40% moves to the 40-th percentile of the length.

Patroon answered 5/10, 2023 at 10:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.