-
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:
Code:
[PRODUCT]
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:
Code:
#!/bin/bash
CDKEY="NEWCDKEY"
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:
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:
Code:
#!/bin/bash
CDKEY="NEWCDKEY"
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
Code:
sed -i s/CDKey.*/CDKey\ =\ \"BYEBYE\"/ cdkeys.ini
will output
which would be perfect if only we'd use a variable!
AnyOne Knows???
Thank you for your attention!
-
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 ...
-
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!
-
Before I forget... how do I mark a thread "solved" ???
for this and future times....
-
NEWKEY="0123456789"
sed -i s"/^CDKey.*/CDKEY = \"${NEWKEY}\"/" cdkeys.ini
Above codes worked, the difference is ' and ".
Have a nice day.
-
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!