add a number to the beginning of every line

hey,

i would like to add a line number to the beginning like so:

red 
blue
green
yellow

will be:

1=>red 
2=>blue
3=>green
4=>yellow

please advise
thank u.

What Operating System and version you you have?
What Shell or Programming Language do you use?

Do you actually want the characters ">=" in the output?
How many records? Unix Shell has trouble counting above 2^23 .

thank u for your reply
i use red hat i think it's the latest version
using perl ,sed, awk , bash or nl will be fine
and yes i do want ">=" included

Unix provided a nice utilty called nl (number line) see if that could help
you. If not see if this works for you

 
cat xx.data
 
red
hello world
green
blue ribbon winner

 
cat xx.ksh
 
#!/bin/ksh
let lineno=0
while read dataline
do
   let lineno=lineno+1
   echo "$lineno=> $dataline"
done < ./xx.data

 
./xx.ksh
 
1=> red
2=> hello world
3=> green
4=> blue ribbon winner

1 Like

See if your version of nl supports the -s option:

nl -s '=>' myfile > mynewfile

IT should have that option, because -s is part of the POSIX standard.

1 Like

The Perl way.

perl -pe 'BEGIN{$i=1}s/^/$i=>/;$i++' inputfile.txt
1 Like