Find the answer to your Linux question:
Results 1 to 6 of 6
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Sed is crushing me

    Hi All...
    Okk, I'll do my best to explain my little mess... !

    We are coding in bash, but we have to interact with an "ugly-old-style" windoze .ini file.

    Suppose we have a file named cdkeys.ini, whose contents look like this:

    CDKey = "ABCD1234"
    Please note that the double quotes around ABCD1234 are important to us.

    Now, suppose we want to change the CDKey... I tried what follows:

    sed -i s/CDKey.*/CDKey\ =\ \"$CDKEY\"/ cdkeys.ini
    Which, I belive, should look for any line with "CDKey" inside, and replace the whole line with CDKey = "NEWCDKEY".

    Actually it doesn't work... it does target the correct line, but all I get is this crap:
    "DKey = "NEWCDKEY
    probably I'm missing something important with the use of sed or regexp or shell expansion... but I noticed a couple things. it works almost good if we remove the \" and \" in the replacement string:

    sed -i s/CDKey.*/CDKey\ =\ $CDKEY/ cdkeys.ini
    leads to


    it also works well if we remove the $CDKEY variable, or replace it with a simple literal like BYEBYE.... so, for example

    sed -i s/CDKey.*/CDKey\ =\ \"BYEBYE\"/ cdkeys.ini
    will output

    CDKey = "BYEBYE"
    which would be perfect if only we'd use a variable!

    AnyOne Knows???

    Thank you for your attention!

  2. #2
    I am a noob in linux ... but I guess I see where the problem is ...
    still don't know what to do against is.
    It seems that in the string of $CDKEY is the symbol for "back to start of line"
    so sed is doing so and writes the second " at the beginning of the line.

    Since I don't know how to look into this in detail my tip is to try using a different editor to look at the shell ...

  3. #3
    Thanks for your reply

    Uhmmm, yes, I am suspicious about that $ as well...
    I looked for an alternate way to expand a variable, but I couldn't find one... the $ is always there... while waiting for illumination, I'm coding an ugly workaround...

    Uhm, different editor? as far as I know bash is always bash, no matter if I code in gedit or another one... or you have been misunderstood

    Hope someone nooooose...

    EDIT: Yeeees!!! my mess, you are right.

    the big problem was not sed or the shell... it was the .ini file to be crippled!
    I thought my ini files were converted from the windows format to the unix format (the cr/lf and unicode stuff...) but I was wrong since a cleanup function was restoring the crippled version...

    opening the .ini files in mc shown that clearly!

  4. $spacer_open
  5. #4
    Before I forget... how do I mark a thread "solved" ???
    for this and future times....

  6. #5
    Just Joined!
    Join Date
    Jan 2010
    sed -i s"/^CDKey.*/CDKEY = \"${NEWKEY}\"/" cdkeys.ini

    Above codes worked, the difference is ' and ".

    Have a nice day.

  7. #6
    Oh Thanx, I see...
    I am unsure... I mean I don't know sed and regexp that much... I'll get into it after work, so I will be sure too!

    Thanks Again in the meanwhile!

Posting Permissions

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