What command to use?

L

laurentv

Guest
I try to remove the '0' at the beginning

01 -> 1
02 > 2
...
09 -> 9
10 > 10
11 > 11
...
99 > 99

Please
Help :)
 


Assuming that these numbers are in a text file and the numbers in question are at the start of each line, this should work:
Code:
sed 's/^[0]*//' yourfile.txt
Note: replace yourfile.txt with whatever your file is called. Also note, the above command will only output the results to the screen, it doesn't write anything to the file.
If you want to overwrite the existing file, you should use the -i option:
Code:
sed -i 's/^[0]*//' yourfile.txt

Or if you want to keep the original you could redirect the output to a new file:
Code:
sed 's/^[0]*//' yourfile.txt > newfile.txt

So if your text file has a list of tasks in it which look something like this:
Code:
01 - Do thing 1
02 - Do thing 2
03 - Do another thing
04 - Wibble
05 - blah blah blah
06 - etc
07 - etc
00008 - etc
000000000009 - etc
010 - etc
011 - end

After running the command, the file should look like this:
Code:
1 - Do thing 1
2 - Do thing 2
3 - Do another thing
4 - Wibble
5 - blah blah blah
6 - etc
7 - etc
8 - etc
9 - etc
10 - etc
11 - end
 
Last edited:
Note: I've just made some edits to my original reply. I forgot that by default, sed only prints output to the screen. So I added some information about how to save the results of the sed operation!
 

Members online


Top