Venkata
Venkata

Reputation: 51

How to add different prefixes at the beginning for different lines in a file

My file has following lines:

hello
hi
hey

I want to use Perl to print letter A at the beginning of the first line, letter B for the second line, letter C for the third line and so on.

My expected output is:

A hello
B hi
C hey

and so on..

I tried the following:

perl -pe 's/^/A/' input.file

This probably will insert only 'A' at the beginning of every line in the file. This doesn't meet my requirement.

I am not sure how to handle different prefixes for different lines.

Upvotes: 0

Views: 32

Answers (1)

choroba
choroba

Reputation: 241828

Increment works for strings, too:

perl -pe 'BEGIN { $ch = "A" } print $ch++, " "' < input_file

(if AA is what follows Z).

Or, if [ follows Z, you can use the $. variable (input line number):

perl -pe 'print chr 64 + $., " "' < input_file

Upvotes: 6

Related Questions