Results 1 to 10 of 11
Hello all, this is my first time posting on this forum, so I hope this is in the right place. I have read a fairly extensive regex tutorial and have ...
- 08-17-2009 #1Just Joined!
- Join Date
- Aug 2009
- Posts
- 6
sed replace won't work
Hello all, this is my first time posting on this forum, so I hope this is in the right place. I have read a fairly extensive regex tutorial and have been looking at numerous sites about sed but can't figure this out.
The following is an except from a text file. I would like to change the 298.0 on the temp line to 573.0:
Afterwards it should look like this:Code:VALUE TYPE DESCRIPTION VARIABLE # 298.0 double Temperature [K] temp
The output file from sed is unchanged, even though RegexBuddy tells me that using the GNU BRE engine this should match. The sed command is here:Code:VALUE TYPE DESCRIPTION VARIABLE # 573.0 double Temperature [K] temp
with twin2.ini being the input file and twin2_temp.ini being the output file. Can anyone give me some insight into why this isn't working?Code:sed -e 's/\(# \)\([0-9]\+\.[0-9]*\)\( *double *Temperature \[K] *temp\)/\1573.0\3/' twin2.ini >twin2_temp.ini
Thanks in advance.
davers
- 08-17-2009 #2
you are escaping things that you shouldnl't be escaping I think
Try this, though i'm not a sed expert, i'll try it as well on my own time and report back.Code:sed -e 's/(# )([0-9]+\.[0-9]*)( *double *Temperature \[K\] *temp)/\1573.0\3/' twin2.ini >twin2_temp.ini
- 08-17-2009 #3Just Joined!
- Join Date
- Aug 2009
- Posts
- 6
I get an error message when I use that command:
I think sed only uses the GNU BRE (Basic Regular Expressions) instead of the GNU ERE (Extended Regular Expressions), so characters like parentheses need to be escaped. At least that is what the error message leads me to believe.Code:sed: -e expression #1, char 68: invalid reference \3 on `s' command's RHS
- 08-17-2009 #4
i was wrong, this works
Code:sed -e 's/\(# *\)\([0-9]*\.*[0-9]*\)\( *double *Temperature \[K\] *temp\)/\1573.0\3/' twin2.ini >twin2_temp.ini
- 08-17-2009 #5Just Joined!
- Join Date
- Aug 2009
- Posts
- 6
From the look of it, it should work; RegexBuddy also says it should work, but for some reason it still doesn't. The output file is the same as the input file. Perhaps I am doing something else wrong? I used that line exactly in the directory that contains twin2.ini, and it creates twin2_temp.ini without changes.
- 08-17-2009 #6
i noticed that, the problem is with the + after the first [0-9], thats what I found anyway, when I changed it to a * it worked, I'm not sure why. Using the * works, but it may not be desireable, perhaps someone with more knowledge of sed could say why, it also could possibly be a bug.
- 08-17-2009 #7Just Joined!
- Join Date
- Aug 2009
- Posts
- 6
That makes sense. When I try your suggestion though, with no +, only *, it still refuses to work even though it looks like it should. It is working for you? Is there a way I can send you the whole input file? Maybe there is something with line breaks that is messing it up.
- 08-17-2009 #8
All I did was copy the sample text and run sed against it, the output was correct with the 2nd post I made.
- 08-17-2009 #9Just Joined!
- Join Date
- Aug 2009
- Posts
- 6
I tried just the sample text I pasted as well, and it worked! I figured out that in the original file there is a tab between "298.0" and "double", but no space. I replaced the spaces with * in the regex and now it works. Thank you for your help!
- 08-17-2009 #10
I think instead of replacing spaces with * you should be able to catch any white space using [ \t]* or maybe just \s* (if that is available)


Reply With Quote