1

I have a file where I'd like to number the lines using "the alphabet" (simple ascii a, b, c, etc.) instead of numbers. So where I could do:

nl somefile

I'd like to do something like:

abc somefile

and get output like:

a line 1 of file
b line 2 of file
c line 3 of file
...

Even an alphabetic equivalent of seq could help -- I could use paste to prefix lines.

To keep things simple, my file would have 26 or less lines (so abc would not need to wrap letters like ...x, y, z, aa, ab, ac... kind of thing.)

1

With bash using paste, head, wc and printf:

# generate somefile
$ printf 'line %s of file\n' {1..5} > somefile

# then paste
$ paste -d ' ' <(printf '%s\n' {a..z}) somefile
a line 1 of file
b line 2 of file
c line 3 of file
d line 4 of file
e line 5 of file
f
g
...

As we don't know how many characters we need to generate (a-e in this example), we could generate the full alphabet and only merge as much lines as somefile has and write it down as a function:

function abc() { paste -d ' ' <(printf '%s\n' {a..z} | head -n $(wc -l <"$1")) "$1"; }

Output:

$ abc somefile
a line 1 of file
b line 2 of file
c line 3 of file
d line 4 of file
e line 5 of file
3

You can use awk:

awk '{printf "%c\t%s\n", NR+96, $0}'

(97 being the ASCII decimal value of a)

% seq 1 10 |  awk '{printf "%c\t%s\n", NR+96, $0}'   
a       1
b       2
c       3
d       4
e       5
f       6
g       7
h       8
i       9
j       10
  • What happens on line 27? – glenn jackman Jun 16 at 14:04
  • @glennjackman doesn't matter ("To keep things simple, my file would have 26 or less lines") – muru Jun 16 at 15:41
0

In bash to print any range of characters, not just alphabetic or numeric:

$ printf '%b\n' "$(eval printf '\\%03o' $(printf '{%d..%d}' "'!" "'~"))"
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
0

Perl knows how to increment strings, including "z" -> "aa" etc. Try this:

# a bash function for "alphabetic nl"
anl() {
    perl -sne 'printf "%6s\t%s", $nl++, $_' -- -nl=a "$@"
}

Then you can do

seq 50 | anl
anl file file ...
  • Unfortunately that's a botch, as it only works in one direction (decrementing aa will not give you z). And its kind of a useless feature that you should take care to avoid, which may be pretty hard as perl doesn't have any kind of type safety. – mosvy Jun 16 at 14:15
  • I don't understand your comment. What does decrementing have to do with this question? Nothing. And, as we can see from this, it's far from useless. It's OK if you don't like perl, but don't denigrate it when it's a right tool for a job. – glenn jackman Jun 16 at 14:31
  • It has anything to do with this feature. People should be aware of its limitations. The OP had limited their problem to files with less 26 lines, so if anything, your answer is off-topic. As to me denigrating perl, you're presuming quite a lot about me. – mosvy Jun 16 at 14:34
  • And I'm not in the mood now to do the research for you, but I think that this feature has been on the deprecation list since quite a while, and it's very easy to replicate with simple use of chr and use overload (and let it work consistently). – mosvy Jun 16 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.