Regex Newcomer
Regex Newcomer

Reputation: 3

Delete numbers from a file

I have a file of the form

00040 void FormExample::setLanguage(const std::string lang)
00041 {
00042   bool haveLang = false;
00043

I want to remove the numbers from the file so that it can compile. I tried using sed -e 's/^(\d)*//g' test.cpp but was not successful. Please tell me what was I doing wrong.

It can be any other standard unix tool besides sed. Shell script, python script, awk etc will also be considered.

Thanks

Upvotes: 0

Views: 357

Answers (3)

Felix Kling
Felix Kling

Reputation: 816472

This works:

sed -e 's/^[0-9]*//' test.cpp

sed's regex flavor is a bit different than Perl's. You don't have this "shorthand" character classes ( e.g. \d or \w) and grouping is done by \( \). The parenthesis in your expression match literally.

Upvotes: 4

Artsiom Rudzenka
Artsiom Rudzenka

Reputation: 29113

In case when each line started with 5 digits and space:

res = [line[6:] for line in open(fileName, 'r')]

f = open(fileName, 'w')
for line in res:
    f.write(line+'\n')
f.close()

Upvotes: 0

cadrian
cadrian

Reputation: 7376

sed -r 's/^[[:digit:]]*//g'

should do it

Upvotes: 1

Related Questions