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


Groups > alt.os.linux > #59155 > unrolled thread

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

Started byGuadalupe <Guadalupe@Guadalupe.com>
First post2019-10-06 13:17 -0400
Last post2019-10-07 13:08 -0400
Articles 12 — 8 participants

Back to article view | Back to alt.os.linux


Contents

  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

#59155 — Adding line numbers to vi (or vim) file of data

FromGuadalupe <Guadalupe@Guadalupe.com>
Date2019-10-06 13:17 -0400
SubjectAdding line numbers to vi (or vim) file of data
Message-ID<qnd7jr$i69$1@gioia.aioe.org>
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?

[toc] | [next] | [standalone]


#59158

FromPaul <nospam@needed.invalid>
Date2019-10-06 14:04 -0400
Message-ID<qndabb$els$1@dont-email.me>
In reply to#59155
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

[toc] | [prev] | [next] | [standalone]


#59168

FromGuadalupe <Guadalupe@Guadalupe.com>
Date2019-10-06 18:16 -0400
Message-ID<qndp42$1462$1@gioia.aioe.org>
In reply to#59158
 said thus to which I respond:

> 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.

I want to do it inside of VIM using VIM macros.

Not in the command line.

Not in some other text editor.

It's ok if it's impossible to do it inside of vim only.

I was just asking for hints how to do it INSIDE of VIM.

If it's possible.

[toc] | [prev] | [next] | [standalone]


#59162

FromChris Elvidge <chris@mshome.net>
Date2019-10-06 19:58 +0100
Message-ID<qnddh5$23v$1@dont-email.me>
In reply to#59155
On 06/10/2019 18:17, 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?
> 

man nl


-- 

Chris Elvidge, England

[toc] | [prev] | [next] | [standalone]


#59178

FromJasen Betts <jasen@xnet.co.nz>
Date2019-10-07 08:12 +0000
Message-ID<qnes0j$7tm$2@gonzo.revmaps.no-ip.org>
In reply to#59155
On 2019-10-06, Guadalupe <Guadalupe@Guadalupe.com> wrote:


> Given I have a file of about 100 lines, I just want line numbers in front
> of each line.

> Any ideas?

cat -n

-- 
  When I tried casting out nines I made a hash of it.

[toc] | [prev] | [next] | [standalone]


#59190

Fromgazelle@shell.xmission.com (Kenny McCormack)
Date2019-10-07 12:32 +0000
Message-ID<qnfb8t$bdn$1@news.xmission.com>
In reply to#59178
In article <qnes0j$7tm$2@gonzo.revmaps.no-ip.org>,
Jasen Betts  <jasen@xnet.co.nz> wrote:
>On 2019-10-06, Guadalupe <Guadalupe@Guadalupe.com> wrote:
>
>
>> Given I have a file of about 100 lines, I just want line numbers in front
>> of each line.
>
>> Any ideas?
>
>cat -n

This is one of those threads where the key is whether or not the OP is
still engaged.  I.e., as of now, it kind of looks like a "drive-by posting".

That said, we have heard from the OP exactly once since the IP.  He
clarified that he is *NOT* looking for any kind of command line solution;
he wants something built-in to VIM.  Ideally, it sounds like he wants a VIM
script-or-macro solution.  In particular, the result must look exactly like
it does in VIM; note that a lot of the command-line solutions posts so far
don't meet this requirement.

That all said, it is certainly possible that an intrepid VIM scripter
(which is to say, not me) could come up with a VIM script-or-macro that
would invoke cat/awk/perl/Eiffel/Ruby/whatever under the covers and produce
the result the OP seeks.

Finally, another key to coming up with a solution that suits the OP is
to know which of the following categories of problem the OP is actually
involved in:
    1) One-time.  I.e., I just want to do this once, with this one specific
	file, and then I'm done with it.
    2) On-going, but only going to be used/done by me (or, possibly, me and
	one or two close associates).
    3) It is part of an application - that will be used by other people,
	maybe even people I don't know - thus it has to be "slick".

-- 
The randomly chosen signature file that would have appeared here is more than 4
lines long.  As such, it violates one or more Usenet RFCs.  In order to remain
in compliance with said RFCs, the actual sig can be found at the following URL:
	http://user.xmission.com/~gazelle/Sigs/Noam

[toc] | [prev] | [next] | [standalone]


#59195

FromGuadalupe <Guadalupe@Guadalupe.com>
Date2019-10-07 09:56 -0400
Message-ID<qnfg6t$t5b$1@gioia.aioe.org>
In reply to#59190
 said thus to which I respond:

>     2) On-going, but only going to be used/done by me (or, possibly, me and
> 	one or two close associates).

I do it every once in a while when I'm making lists.
Maybe on average once a month.
I do it manually now.
I just wanted to know if vim could do it.
That's all.

It needs to work on both linux and windows.
That's why it needs to be inside of vim.
I didn't ask windows people because they mostly use notepad.

I'm ok if it's impossible.
I've been looking for years.

I just figured I'd ask.
Just in case someone else has figured it out already.

[toc] | [prev] | [next] | [standalone]


#59199

Fromgazelle@shell.xmission.com (Kenny McCormack)
Date2019-10-07 14:31 +0000
Message-ID<qnfi7e$gi9$1@news.xmission.com>
In reply to#59195
In article <qnfg6t$t5b$1@gioia.aioe.org>,
Guadalupe  <Guadalupe@Guadalupe.com> wrote:
> said thus to which I respond:
>
>>     2) On-going, but only going to be used/done by me (or, possibly, me and
>> 	one or two close associates).
>
>I do it every once in a while when I'm making lists.
>Maybe on average once a month.
>I do it manually now.
>I just wanted to know if vim could do it.
>That's all.
>
>It needs to work on both linux and windows.
>That's why it needs to be inside of vim.
>I didn't ask windows people because they mostly use notepad.

Thank you for the clarification.

If I were doing this for myself, I'd lean on some existing utility
(presumably, from the Unix world, but there are Windows versions available
nowadays for most of them) and then wrap it up in a VIM macro.  In fact, I
have versions of AWK available on all of my Windows machines, so that'd be
what I use.  I say all this because I find VIM scripting ugly and, in
almost all cases, unnecessary.  I've done enough VIM scripting to both know
how to do it and know that I don't want to do any more than that which is
absolutely necessary.

But for you, given that you don't seem comfortable with that approach, what
I would do is post on the vim-use mailing list - which is pretty active and
is filled with eager helpers who like writing VIM script.  You could
probably get an answer there - something written entirely in VIM script and
which would work on any platform, independent of any external tools.

    :help maillist

will get you started...

-- 
The plural of "anecdote" is _not_ "data".

[toc] | [prev] | [next] | [standalone]


#59183

FromDan Purgert <dan@djph.net>
Date2019-10-07 10:16 +0000
Message-ID<slrnqpm44k.1ug.dan@djph.net>
In reply to#59155
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

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.

You seem to have two slightly different wants here, at least as I read
it:

  1. :set number -- to get (ephemeral) line numbers, merely placeholders
      for reference later (e.g. "Guadalupe, on line 18, you should have
      'x=y;' but you have 'y=x;'")

  2. Itemizing a list, such as this 2-item list I've created in this
      post.

Can you clarify things a bit?


-----BEGIN PGP SIGNATURE-----

iQEzBAEBCAAdFiEEBcqaUD8uEzVNxUrujhHd8xJ5ooEFAl2bEJMACgkQjhHd8xJ5
ooExvQf/XXWeE6wc6pTygnaVc/AIWd+RP3y6Y2wsqtsHYShi7jykSMM30046mvPA
A2gvh3k76PairQWojPZVXsgfbUw/srZXMqFAscikNTlksTnItBxw8UO4L9UFcXWY
w5mRWzPrzCnBfCDn9fWQbQSo4rAj84Pioel8TwqxxO/20n1u14m42LuLw+L6Ir2M
No1yfq5ydgSW2WCjsR65xLXTOlV5F920MQW3ExzlIwXKqzFAK+hkgaDtdyJjcYXz
FeCifmKOGjFoutlDoE7vnXA6+AnN2itO8avzc2TSBY9E6CYs8/7mGFz6qrS/2iMY
zkjmLeGmxClKhRrgNmj2jaLouJACIA==
=J3Rk
-----END PGP SIGNATURE-----

-- 
|_|O|_| 
|_|_|O| Github: https://github.com/dpurgert
|O|O|O| PGP: 05CA 9A50 3F2E 1335 4DC5  4AEE 8E11 DDF3 1279 A281

[toc] | [prev] | [next] | [standalone]


#59196

FromReal Troll <real.troll@trolls.com>
Date2019-10-07 09:59 -0400
Message-ID<qnfgh8$umn$1@gioia.aioe.org>
In reply to#59155
On 06/10/2019 18:17, Guadalupe wrote:
> Given I have a file of about 100 lines, I just want line numbers in front
> of each line.
>
> Any ideas?

Use VS code for Linux.

[toc] | [prev] | [next] | [standalone]


#59198

Frommarrgol <marrgol@address.invalid>
Date2019-10-07 16:25 +0200
Message-ID<5d9b4af7$0$521$65785112@news.neostrada.pl>
In reply to#59155
On 2019-10-06 at 19:17, 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?

https://vim.fandom.com/wiki/Insert_line_numbers
(unless what you really want is the numbers to automatically change
while you insert/delete lines.)


-- 
mrg

[toc] | [prev] | [next] | [standalone]


#59208

FromGuadalupe <Guadalupe@Guadalupe.com>
Date2019-10-07 13:08 -0400
Message-ID<qnfrde$jjt$1@gioia.aioe.org>
In reply to#59198
 said thus to which I respond:

> https://vim.fandom.com/wiki/Insert_line_numbers
> (unless what you really want is the numbers to automatically change
> while you insert/delete lines.)

That's it!

 %s/^/\=printf('%-4d', line('.'))

For each line of the file [%]
Search for the beginning of the line [s/^]
And replace it with [/]
The evaluation of the following expression [\=]

Where the following expression is contained inside [(...)]
Print formatted a left-aligned decimal number padded to 4 columns [%-4d]
And then [, ]
The line itself [line('.')]

Where %-4d is a left-aligned decimal number padded with spaces
And where %4d is right aligned
And %04d inserts leading zeroes
And %d.\t inserts a left-aligned number followed by a period and tab

The article lists how to format a range of lines within the file
 :'<,'>s/^/\=printf("%d.\t", line(".") - line("'<") + 1)
Or, after running :let i = 1
 :'<,'>g/^\S/s/^/\=printf("%d.\t", Inc())

I will do it more consistently, just by adding "mark x" & "mark y".

First, I'd mark the top of the section to be numbered [mx]
Then, I'd mark the bottom of the section to be numbered [my]
Then I'd run the same command as before, from x to y, as shown below
 :'x,'ys/^/\=printf('%-4d', line('.'))
Where I'd just replace the whole file [%] with the section ['x,'y]

Thank you for showing how to add sequential line numbers inside of vi.

[toc] | [prev] | [standalone]


Back to top | Article view | alt.os.linux


csiph-web