Search code examples
perlline-numbers

How do I assign the line number to each string present in a file


F1.txt

tom a b c d e boy

bob a b c sun

harry a c d e girl

result

F2.txt

tom1 a1 b1 c1 d1 e1 boy1

tom2 a2 b2 c2 sun2

tom3 a3 c3 d3 e3 girl3

Hello everyone, I am quite new to Perl,can you kindly help me out with this new problem of mine. I have a file F1.txt, my job is to assign numbers after each string in a file according to its line number as shown in an example above. I have so far just managed to assign a number to each of the lines with this Perl one-liner

perl -pe '$_= ++$a." $_" if /./'

Solution

  • Maybe as follows:

    perl -pe 's/(?<=\w)\b/$./g;'