How to change only the x first characters of a string?

Hi,

How can I replace x first characters from a string?
I have a file with... say 10000 entries as follows:

00123456781
00123456782
00123456783
...

What I want to do is change the leading "00" with for example "12"

The leading 00 can be in some files some other 1 or more digits e.g. "1" or "123" or what ever.

What would be the most efficient way of doing this as there might be even 20.000.000 entries in one file.

I wonder if the method could be embedded to a oneliner such as:

perl -i -p -e "s/old/new/g" filename

Thanks!

hey,

You can user sed.

sed 's/[1]*/your replace string or number/' filename

I think you can use this when no. starts with 00* and in the starting of the line.

Otherwise you can use tr command to squeeze the multiple 0s.
visit: man tr

Thanks !!:slight_smile:


  1. 00 ↩︎

Thanks :slight_smile: I actually took the regular expression out your solution and included it into the perl one liner and it took 1.6 seconds to replace 1.000.000 entries!

Thanks for your help