"less" (linux) get/go-to X offset in file

8

I open a big file with this command: less +G /var/log/blah/file.log

Now when I find the desired info I'd like to know its exact offset in this file, so later I can open that file again and return to that exact place.

So there is the "%X" command which will take me to % inside the file, in offset terms (and not lines!). It's fine, but I'd really love to be able to move to exact, numeric, offset in the file.

What is the command to get the current offset?
And what is the command to move to a specific offset?

And if I'm at that already, how do I get the current offset in percent? (to be used with the "%" command).

Note that I know of marks. They aren't what I'm looking for.

Poni

Posted 2011-12-28T11:06:19.870

Reputation: 453

Answers

16

Got part of my question from here:

100g to go to the 100th line
50p to go to 50% into the file
100P to go to the line containing 100th byte

To determine the current line number or byte offset, use Ctrl+g.

Poni

Posted 2011-12-28T11:06:19.870

Reputation: 453

For very large files, you may also want to invoke less with -n to avoid calculating line numbers. – Miles – 2019-01-09T01:47:35.580

3Found it: Ctrl+g ! – Poni – 2012-01-16T14:15:09.300

0

With my version the lines currently on screen are displayed at the bottom.

To go to a specific line you can type <number>G.

Rob Wouters

Posted 2011-12-28T11:06:19.870

Reputation: 356

Since the file is very big (more than GB) I'm not even using lines. I need to deal with offset within the file only. Thanks Rob – Poni – 2011-12-28T11:14:09.267

0

Assuming that by 'offset' you mean byte-offset (as opposed to characters), you can use the vi editor and search for the pattern with

/pattern

hit Enter. Repeated fwd searching for the same pattern can be repeated with

/

hit Enter.

Cursor position's line and column numbers are displayed at bottom right, so to go to a particular line, say 201, and column, say 17, you type

201G17l

That's 201, capital G, 17, lowercase l

If your file consists of only 1 line with your pattern at offset 10125, then open it with vi

$ vi my_massive_file

and type

:goto 10125

venzen

Posted 2011-12-28T11:06:19.870

Reputation: 319

i noticed after posting that your file is > 1GB and that is probably why you are using less. vi should be able to handle it if you have enough memory – venzen – 2011-12-28T12:15:42.427

Yea, it opens way too slow with vi/m. Not good for me. And yes also to your assumption; I mean "offset" as in bytes within the file. – Poni – 2011-12-28T12:52:50.820