Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > alt.os.linux > #59158

Re: Adding line numbers to vi (or vim) file of data

From Paul <nospam@needed.invalid>
Newsgroups alt.os.linux
Subject Re: Adding line numbers to vi (or vim) file of data
Date 2019-10-06 14:04 -0400
Organization A noiseless patient Spider
Message-ID <qndabb$els$1@dont-email.me> (permalink)
References <qnd7jr$i69$1@gioia.aioe.org>

Show all headers | View raw


Guadalupe wrote:
> Given I have a file of about 100 lines, I just want line numbers in front
> of each line.
> 
> If I use ":set number" in vi, I see EXACTLY what I want but I can't copy
> and paste the result to create a text file that has actual numbers for each
> item.
> 
> Googling for how to insert a sequence of numbers from 1 to 100, one in
> front of each line, I find plenty of stuff that simply numbers an empty
> file such as these
> https://vim.fandom.com/wiki/Generating_a_column_of_increasing_numbers
> https://stackoverflow.com/questions/9903660/vi-how-to-generate-a-number-sequence
> https://vim.fandom.com/wiki/Making_a_list_of_numbers
> 
> But I don't want to "make a list of sequential numbers".
> I want to number the list of lines that I already have.
> 
> Any ideas?

If a text editor "auto-wraps" the text to a certain
screen width, it may be difficult to get other software
to do it exactly the same way. That's if you insist on your
line numbering exercise to somehow take wrap into account
or something.

It's easier to apply one line number per line which terminates
with line termination character(s).

You can use AWK ("gawk") or PERL to script a solution.

******* number.awk *******

# gawk -f number.awk input.txt > output.txt

BEGIN {
   i = 1
}

{ printf("%05d: %s\n", i++, $0 ) }

******* end number.awk *******

gawk -f number.awk input.txt > output.txt

The quick brown fox jumped
over the lazy hen
scrawl and the dish
ran away with the spoon.

00001: The quick brown fox jumped
00002: over the lazy hen
00003: scrawl and the dish
00004: ran away with the spoon.

My little "script", is to illustrate how easy it
is to do in trivial cases. That's not a finished
program by any means, just shows the power of text
scripting languages. If line wrap were involved,
I can see a lot of work being necessary to make it
look exactly like the screen.

HTH,
    Paul

Back to alt.os.linux | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Adding line numbers to vi (or vim) file of data Guadalupe <Guadalupe@Guadalupe.com> - 2019-10-06 13:17 -0400
  Re: Adding line numbers to vi (or vim) file of data Paul <nospam@needed.invalid> - 2019-10-06 14:04 -0400
    Re: Adding line numbers to vi (or vim) file of data Guadalupe <Guadalupe@Guadalupe.com> - 2019-10-06 18:16 -0400
  Re: Adding line numbers to vi (or vim) file of data Chris Elvidge <chris@mshome.net> - 2019-10-06 19:58 +0100
  Re: Adding line numbers to vi (or vim) file of data Jasen Betts <jasen@xnet.co.nz> - 2019-10-07 08:12 +0000
    Re: Adding line numbers to vi (or vim) file of data gazelle@shell.xmission.com (Kenny McCormack) - 2019-10-07 12:32 +0000
      Re: Adding line numbers to vi (or vim) file of data Guadalupe <Guadalupe@Guadalupe.com> - 2019-10-07 09:56 -0400
        Re: Adding line numbers to vi (or vim) file of data gazelle@shell.xmission.com (Kenny McCormack) - 2019-10-07 14:31 +0000
  Re: Adding line numbers to vi (or vim) file of data Dan Purgert <dan@djph.net> - 2019-10-07 10:16 +0000
  Re: Adding line numbers to vi (or vim) file of data Real Troll <real.troll@trolls.com> - 2019-10-07 09:59 -0400
  Re: Adding line numbers to vi (or vim) file of data marrgol <marrgol@address.invalid> - 2019-10-07 16:25 +0200
    Re: Adding line numbers to vi (or vim) file of data Guadalupe <Guadalupe@Guadalupe.com> - 2019-10-07 13:08 -0400

csiph-web