Insert ongoing numeration in each line?

Hi.

I have a text file with lines like this:

( chp_testfile_0065 "Blablabla." )
( chp_testfile_0003 "Blablabla" )
( chp_testfile_0123 "Blablabla" )

I need one more 0 in each line, so that I have 5 digits in a sequence. I also need an ongoing numeration from 1 to n, so that

( chp_testfile_00001 "Blablabla." )
( chp_testfile_00002 "Blablabla" )
( chp_testfile_00003 "Blablabla" )

Does anyone have an idea how I can accomplish this?

I have a php script, which once did that for me..but with different positions...but I don't know php at all and don't understand it, so I can't rewrite it to suit my needs. I could copy and paste it if that helps!

Thanks,

Kat

Try:

perl -pe '$j=sprintf "%05d",++$i;s/\d+ "/$j "/' file
1 Like

Thanks! How would I do this, if I don't want the numeration to start from 1, but from e.g. 14000?

perl -pe 'BEGIN{$i=14000};$j=sprintf "%05d",$i++;s/\d+ "/$j "/' file

Thank you :slight_smile:

I was trying to use the same code for a similar textfile, which doesn't have the double quotes, so:

( chp_testfile_0065 Blablabla. )
( chp_testfile_0003 Blablabla )
( chp_testfile_0123 Blablabla )

But it didn't work. Is there a way this code can be adjusted?

This should do.

 
perl -pe '$j=sprintf "%05d",++$i;s/\d+ /$j /'

Unfortunately it didn't change anything.

Either you do something wrong or your input is different.

% cat > testfile
( chp_testfile_0065 Blablabla. )
( chp_testfile_0003 Blablabla )
( chp_testfile_0123 Blablabla )

% perl -pe '$j=sprintf "%05d",++$i;s/\d+ /$j /' testfile 
( chp_testfile_00001 Blablabla. )
( chp_testfile_00002 Blablabla )
( chp_testfile_00003 Blablabla )

Works here. See the output

 
perl -pe '$j=sprintf "%05d",++$i;s/\d+ /$j /' te1
( chp_testfile_00001 Blablabla. )
( chp_testfile_00002 Blablabla )
( chp_testfile_00003 Blablabla )

I honestly have no clue what's going wrong here. I'm not doing this for the first time, and the way I'm doing it has worked before.

When I run the command

perl -pe '$j=sprintf "%05d",++$i;s/\d+ "/$j "/' q6.txt

the file looks exactly like it looked before.

I attached the file, it would be great if you could have a look at it. Thanks!

Remove the double quotes in the script.

 
perl -pe '$j=sprintf "%05d",++$i;s/\d+ /$j /' q6.txt

It's not enough, there are tabs after number, so:

perl -pe '$j=sprintf "%05d",++$i;s/\d+\s/$j /' q6.txt
1 Like