Unix Shell - Help: Line Number

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > May 2007 > Help: Line Number





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Help: Line Number
Amy Lee

2007-05-18, 7:20 am

Hi,

I'm a newbie in shell scripts. And I have a problem about line number.

Now I have a list of database name, just like this.

A
B
C
D
E

And I hope that I can use some shell skills to make the line number like
this:

1 A
2 B
3 C
4 D
5 E

Could you tell me how to do that?

Thanks in advance!

Amy Lee
Michael Heiming

2007-05-18, 7:20 am

In comp.unix.shell Amy Lee <openlinuxsource@gmail.com>:
[..]
> Now I have a list of database name, just like this.


> A
> B
> C
> D
> E


> And I hope that I can use some shell skills to make the line number like
> this:


> 1 A
> 2 B


awk '{print NR,"\t",$0}' infile

or just

cat -n infile

Within vi something like this should do the trick:

:%call setline(line("."), line(".") . "\t" . getline(line(".")))

Good luck

--
Michael Heiming (X-PGP-Sig > GPG-Key ID: EDD27B94)
mail: echo zvpunry@urvzvat.qr | PERL -pe 'y/a-z/n-za-m/'
#bofh excuse 442: Trojan horse ran out of hay
Geoff Clare

2007-05-18, 1:23 pm

Michael Heiming wrote:

>
>
> awk '{print NR,"\t",$0}' infile


That would put an extra space before and after the tab. To separate
with just a tab:

awk '{print NR "\t" $0}'

> or just
>
> cat -n infile


Non-standard. Standard equivalents are:

pr -tn infile

nl -ba infile

> Within vi something like this should do the trick:
>
> :%call setline(line("."), line(".") . "\t" . getline(line(".")))


Only in vim. No other version of vi would understand that.

--
Geoff Clare <netnews@gclare.org.uk>
Michael Heiming

2007-05-18, 1:23 pm

In comp.unix.shell Geoff Clare <geoff@clare.see-my-signature.invalid>:
> Michael Heiming wrote:

[..]

[vbcol=seagreen]
[vbcol=seagreen]
> Only in vim. No other version of vi would understand that.


Likely, though I wasn't completely sure and couldn't be bothered
to double check, it was obvious anyway someone would point out
quite soon. ;-)

--
Michael Heiming (X-PGP-Sig > GPG-Key ID: EDD27B94)
mail: echo zvpunry@urvzvat.qr | PERL -pe 'y/a-z/n-za-m/'
#bofh excuse 449: greenpeace free'd the mallocs
Amy Lee

2007-05-19, 7:18 am

On Fri, 18 May 2007 15:15:27 +0200, Michael Heiming wrote:

> In comp.unix.shell Geoff Clare <geoff@clare.see-my-signature.invalid>:
> [..]
>
>
>
>
> Likely, though I wasn't completely sure and couldn't be bothered
> to double check, it was obvious anyway someone would point out
> quite soon. ;-)



Thank you very much!
ramesh.thangamani@gmail.com

2007-05-19, 1:20 pm

On May 18, 4:20 pm, Amy Lee <openlinuxsou...@gmail.com> wrote:
> Hi,
>
> I'm a newbie in shell scripts. And I have a problem about line number.
>
> Now I have a list of database name, just like this.
>
> A
> B
> C
> D
> E
>
> And I hope that I can use some shell skills to make the line number like
> this:
>
> 1 A
> 2 B
> 3 C
> 4 D
> 5 E
>
> Could you tell me how to do that?
>
> Thanks in advance!
>
> Amy Lee


You want to just print line numbers?. There are different ways of
doing this:

1. cat -n <filename>
2. awk '{ print NR, $0 }' <filename>
3. grep -n '^' <filename>
4. sed '=' <filename> | | sed 'N;s/\n/ /'

Hope this helps.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com