Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
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 ...
  1. #1
    Just 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:

    Code:
    VALUE       TYPE    DESCRIPTION                             VARIABLE
    # 298.0     double  Temperature [K]                         temp
    Afterwards it should look like this:

    Code:
    VALUE       TYPE    DESCRIPTION                             VARIABLE
    # 573.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:
    sed -e 's/\(# \)\([0-9]\+\.[0-9]*\)\( *double *Temperature \[K] *temp\)/\1573.0\3/' twin2.ini >twin2_temp.ini
    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?

    Thanks in advance.

    davers

  2. #2
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    you are escaping things that you shouldnl't be escaping I think
    Code:
    sed -e 's/(# )([0-9]+\.[0-9]*)( *double *Temperature \[K\] *temp)/\1573.0\3/' twin2.ini >twin2_temp.ini
    Try this, though i'm not a sed expert, i'll try it as well on my own time and report back.

  3. #3
    Just Joined!
    Join Date
    Aug 2009
    Posts
    6
    I get an error message when I use that command:

    Code:
    sed: -e expression #1, char 68: invalid reference \3 on `s' command's RHS
    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.

  4. #4
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    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

  5. #5
    Just 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.

  6. #6
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    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.

  7. #7
    Just 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.

  8. #8
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    All I did was copy the sample text and run sed against it, the output was correct with the 2nd post I made.

  9. #9
    Just 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!

  10. #10
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    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)

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...