Bloomy
June 16, 2011, 5:27pm
1
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
Bloomy
June 17, 2011, 2:28pm
3
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
Bloomy
June 22, 2011, 6:42am
6
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?
getmmg
June 22, 2011, 6:46am
7
This should do.
perl -pe '$j=sprintf "%05d",++$i;s/\d+ /$j /'
Bloomy
June 22, 2011, 6:51am
8
Unfortunately it didn't change anything.
yazu
June 22, 2011, 7:23am
9
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 )
getmmg
June 22, 2011, 7:32am
10
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 )
Bloomy
June 22, 2011, 7:36am
11
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!
getmmg
June 22, 2011, 7:48am
12
bloomy:
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
yazu
June 22, 2011, 7:53am
13
It's not enough, there are tabs after number, so:
perl -pe '$j=sprintf "%05d",++$i;s/\d+\s/$j /' q6.txt
1 Like