LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Can't replace string with multiple spaces with sed in linux (Slackware). Please help! (https://www.linuxquestions.org/questions/programming-9/cant-replace-string-with-multiple-spaces-with-sed-in-linux-slackware-please-help-4175458949/)

Holering 04-20-2013 08:55 PM

Can't replace string with multiple spaces with sed in linux (Slackware). Please help!
 
Doing
sed -i "s/#DEFAULT_AMPLIFICATION___30/#DEFAULT_AMPLIFICATION___0/g" config.h
does nothing and for every "_" there is an empty space. Due to the forum handling code I can't properly post with real spaces include (they get deleted into one space); perhaps some sort of anti spam mechanism or something to do with linux file-naming in general (assuming this board runs off Linux systems)?

How do I replace the string #DEFAULT_AMPLIFICATION___30 with #DEFAULT_AMPLIFICATION___0? Every "_" is a space.

Slackware64 14.

Kind Regards,

Ser Olmy 04-20-2013 09:18 PM

I can't see anything wrong with that command.

In fact, I just tried it and it seems to work fine here on my 32-bit Slackware 14.0 system (assuming you want to replace all occurrences of #DEFAULT_AMPLIFICATION 30 with #DEFAULT_AMPLIFICATION 0 in the file config.h).

syg00 04-20-2013 10:31 PM

If you have an indeterminate number (or type, e.g. <tab>) you perhaps should use a class such as [[:space:]]+

sed supports regex extended.

Holering 04-20-2013 10:44 PM

Strange. I went to edit my post but sure enough it shows plenty of spaces between "#DEFAULT_AMPLIFICATION 3". Guess there's problems with spaces in this forum too!

"DEFAULT_AMPLIFICATION___3" with every "_" being an empty space is what it should look like in my original post. I've edited my original post if you can still help.

Kind regards,

grail 04-21-2013 01:00 AM

Please use [code][/code] tags to keep all formatting and when showing code or data.

pan64 04-21-2013 02:19 AM

please use
Code:

[code]
sed -i "s/#DEFAULT AMPLIFICATION  30/#DEFAULT AMPLIFICATION  0/g" config.h
[/code]

to keep formatting
and give an example of your config.h what should be replaced and what's happened.
As it was already mentioned it should work.
In case you have tabs also (not only spaces) you may need to use another syntax:
Code:

sed -ri 's/(#DEFAULT AMPLIFICATION\s+)30/\10/g' config.h

Holering 04-21-2013 04:55 AM

Code:

sed -ri 's/(#DEFAULT AMPLIFICATION\s+)0/\10/g' config.h

Used your example but doesn't work... Tried in frame-buffer console but same results.
Trying this with sdl_mixer source code in $SOURCE/timdity/config.h. This is what the section in config.h looks like:

Code:

/* A somewhat arbitrary frequency range. The low end of this will
  sound terrible as no lowpass filtering is performed on most
  instruments before resampling. */
#define MIN_OUTPUT_RATE        4000
#define MAX_OUTPUT_RATE        65000

/* In percent. */
/* #define DEFAULT_AMPLIFICATION        70 */
/* #define DEFAULT_AMPLIFICATION        50 */
#define DEFAULT_AMPLIFICATION  30


grail 04-21-2013 05:00 AM

Well unless I have missed something, none of the lines you have shown in your example match what you are searching for:
Code:

(#DEFAULT AMPLIFICATION\s+)0
Find this string followed by any type of whitespace and then a 0. As all of your examples have another digit prior to the 0, this will never match.

Holering 04-21-2013 05:18 AM

Quote:

Originally Posted by grail (Post 4935703)
Well unless I have missed something, none of the lines you have shown in your example match what you are searching for:
Code:

(#DEFAULT AMPLIFICATION\s+)0
Find this string followed by any type of whitespace and then a 0. As all of your examples have another digit prior to the 0, this will never match.

I'm not sure I follow correctly. Don't understand sed very well.

Tried:
Code:

sed -i 's/(#DEFAULT AMPLIFICATION\s+)30/(#DEFAULT AMPLIFICATION\s+)0/g' config.h
sed -i 's/(#DEFAULT AMPLIFICATION\s+)30/\10/g' config.h
sed -i 's/#DEFAULT AMPLIFICATION  30/#DEFAULT AMPLIFICATION  0/g' config.h

None of these work.

Regards

pan64 04-21-2013 05:58 AM

still not sure what do you need would be much better to say not only it does not work but what would be the expected result. Try this, probably:
Code:

sed -ri 's/(#DEFAULT AMPLIFICATION\s+)\d+/\10/g' config.h
you should use -r , without -r it will not work (sed -i itself is not enough)

grail 04-21-2013 07:27 AM

As indicated by pan64, without -r your current attempts are also looking for the round brackets you have used.

Holering 04-30-2013 09:22 AM

Didn't work. Tried -ri with previous examples but nothing changes.

Trying to change the following line:
Code:

#define DEFAULT_AMPLIFICATION  30
to look like this:
Code:

#define DEFAULT_AMPLIFICATION  0
Regards

grail 04-30-2013 10:05 AM

Well again, I would point out that your example sed's and your before and after data do no match.

You state you start with:
Code:

#define DEFAULT_AMPLIFICATION  30
Yet ALL of your sed examples in post 9 have nothing in common with this:
Code:

sed -i 's/(#DEFAULT AMPLIFICATION\s+)30/(#DEFAULT AMPLIFICATION\s+)0/g' config.h
sed -i 's/(#DEFAULT AMPLIFICATION\s+)30/\10/g' config.h
sed -i 's/#DEFAULT AMPLIFICATION  30/#DEFAULT AMPLIFICATION  0/g' config.h

In order they appear:

1. No -r option so all () will be interpreted as being part of the string which are not there

2. ALL sed searches are looking for lines starting with # and immediately followed by 'DEFAULT'. Again, not in your input data as line starts with '#define'

3. ALL sed's are searching for 'DEFAULT AMPLIFICATION', however the input has an underscore between the words. Again, no match

4. First sed will give incorrect output assuming search worked as '\s+' will be interpreted literally in the second half of sed

As you can see, the issue here is not so much what you are getting wrong with sed, but your expression you are searching for is flawed and hence no changes are being made.

Holering 04-30-2013 03:41 PM

Wow I was having so much fun with jungle bunnies in my head. Grail, you're absolutely right! After focusing I realized using
Code:

sed -ri 's/(#define DEFAULT_AMPLIFICATION\s+)30/#define DEFAULT AMPLIFICATION  0/g' config.h
is the proper command. Didn't know about using () with sed. Great help from you folks. Many thank yous!

Regards

grail 04-30-2013 04:06 PM

Now that you have the correct search string, you can remove the brackets if you are not going to use the back reference (ie. \1). I assume this would be correct as you are removing
the underscore as well as replacing the number 30 to 0.

To give you something else to think about, you can merely search for the line and then do a replace on only those things you need instead of the entire line.
As an example, if we assume your original line:
Code:

#define DEFAULT_AMPLIFICATION  30
For the example we will also assume that DEFAULT_AMPLIFICATION string only appears once in the file and you wish to only replace 30 by 0, you could do the following:
Code:

sed -i '/DEFAULT_AMPLIFICATION/s/3//' config.h
Again you do not have to worry about spacing or what type of whitespace as it is not involved in the equation.

Hope that helps :)

Should you need more details on sed, have a look here - http://www.grymoire.com/Unix/Sed.html


All times are GMT -5. The time now is 12:33 PM.