0

I am a beginner with PYTHON and useless with regex and are struggling to replace an unknown number in a file with a new number. I have looked through the python how tos and examples for re but still can't make any progress on how to create the expression.

# myfile.txt
Some text
More text
Option  "BlankTime" "15"
more text

I want to replace "15" with another number, this line only appears once in the file, but the line number it is on is unknown, and the value of 15 is also unknown and could be any number contained in the quotation marks.

Best way would be to do it with python ( re ?? ) but if that can't be done then maybe sed ??

5
  • 1
    Do you know how to open a file and extract it line by line? Commented Oct 6, 2011 at 10:05
  • Please post the code you have written so far. It helps a lot if you show exactly what you've managed to learn. Commented Oct 6, 2011 at 10:16
  • Thanks for all the suggestions, yes I know how to open a file and loop through it line by line, but I was hoping for a one-liner using an existing function rather than a loop. Commented Oct 6, 2011 at 10:57
  • I actually need to change the text in the file, rather than printing the output to the screen. Commented Oct 6, 2011 at 10:58
  • 1
    @crankshaft: "I know how to open a file and loop through it line by line" We don't know how much you know. Even that doesn't tell us much. Please actually post the actual code you have with a "# don't know what to do here" comment or some other hint. Commented Oct 6, 2011 at 11:30

3 Answers 3

1

It sounds like you're looking for a particular parameter (BlankTime). This can be done with the following sed one-liner:

cat myfile.txt | sed 's/\("BlankTime"\s*"\)[^"]*/\1987/'

This'll search for "BlankTime" and replace its value with 987, leaving all the other lines intact.

edit To replace the contents of the file, use the following two-step approach:

cat myfile.txt | sed 's/\("BlankTime"\s*"\)[^"]*/\1987/' > myfile.txt.tmp
mv myfile.txt.tmp myfile.txt
Sign up to request clarification or add additional context in comments.

5 Comments

Hi, great that's the kind of thing I was hoping for, but I need to actually change the text in the file rather than print the modified output. I have tried echoing the result back into the file, but all of the carriage returns and white spaces have been removed: echo -e $(cat myfile.txt | sed 's/("BlankTime"\s*")[^"]*/\1987/') > myfile.txt
Hi, it almost works, but one of the quotation marks is being stripped from the output: Option 987"
@crankshaft: Are you using the exact command from my post? I get Option "BlankTime" "987"
Hi, I am running this using PYTHON commands, I have escaped the double quotation marks, but can't get it to work as if it were typed via the console which works perfectly: commands.getoutput("cat myfile.txt | sed 's/(\"BlankTime\"\s*\")[^\"]*/\1987/' > myfile.txt.tmp") - The result strips away "BlankTime" and the quotation mark in fron of the number and the output is: Option 987"
OK, solved it, I needed to escape the backslash as well as the quotation marks, thanks so much
1
import re
with open("myfile.txt", "r") as myfile:
    mytext = myfile.read()
pattern = re.compile(r'^(Option\s+"BlankTime"\s+")(\d+)"', re.MULTILINE)
mystr = pattern.sub(r'\1REPLACED"', mytext)
with open("myfile.txt", "w") as myfile:
    myfile.write(mytext)

This will replace all occurrences in the file at once. I've put the number into capturing parentheses in case you'd want to do something with it before the replacement.

1 Comment

Thanks, I am going to try this too,
-1
myfile = open('myfile.txt', 'r')
text = myfile.read()
myfile.close()

print re.sub(r'\d+', '42', text)

\d matches a number, + matches at least one occurrence of the preceding pattern.

2 Comments

It would be safer to match the quotes as well since the text could contain a number: re.sub(r'"(\d+)"', '42', text)
That's your assumption. According to OP's question, it doesn't matter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.