LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-20-2013, 09:55 PM   #1
Holering
Member
 
Registered: Feb 2010
Distribution: Slackware
Posts: 162

Rep: Reputation: 22
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,

Last edited by Holering; 04-20-2013 at 11:50 PM.
 
Old 04-20-2013, 10:18 PM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 2,013

Rep: Reputation: Disabled
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).
 
1 members found this post helpful.
Old 04-20-2013, 11:31 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,425

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

sed supports regex extended.
 
1 members found this post helpful.
Old 04-20-2013, 11:44 PM   #4
Holering
Member
 
Registered: Feb 2010
Distribution: Slackware
Posts: 162

Original Poster
Rep: Reputation: 22
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,

Last edited by Holering; 04-20-2013 at 11:49 PM.
 
Old 04-21-2013, 02:00 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,626

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Please use [code][/code] tags to keep all formatting and when showing code or data.
 
1 members found this post helpful.
Old 04-21-2013, 03:19 AM   #6
pan64
Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 5,003

Rep: Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320
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
 
1 members found this post helpful.
Old 04-21-2013, 05:55 AM   #7
Holering
Member
 
Registered: Feb 2010
Distribution: Slackware
Posts: 162

Original Poster
Rep: Reputation: 22
Question

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
 
Old 04-21-2013, 06:00 AM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,626

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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.
 
1 members found this post helpful.
Old 04-21-2013, 06:18 AM   #9
Holering
Member
 
Registered: Feb 2010
Distribution: Slackware
Posts: 162

Original Poster
Rep: Reputation: 22
Quote:
Originally Posted by grail View Post
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
 
Old 04-21-2013, 06:58 AM   #10
pan64
Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 5,003

Rep: Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320
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)
 
1 members found this post helpful.
Old 04-21-2013, 08:27 AM   #11
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,626

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
As indicated by pan64, without -r your current attempts are also looking for the round brackets you have used.
 
1 members found this post helpful.
Old 04-30-2013, 10:22 AM   #12
Holering
Member
 
Registered: Feb 2010
Distribution: Slackware
Posts: 162

Original Poster
Rep: Reputation: 22
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
 
Old 04-30-2013, 11:05 AM   #13
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,626

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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.
 
1 members found this post helpful.
Old 04-30-2013, 04:41 PM   #14
Holering
Member
 
Registered: Feb 2010
Distribution: Slackware
Posts: 162

Original Poster
Rep: Reputation: 22
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

Last edited by Holering; 04-30-2013 at 04:42 PM.
 
Old 04-30-2013, 05:06 PM   #15
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,626

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sed replace string xmickaelx Programming 3 04-07-2011 07:42 AM
[SOLVED] Using Sed to search and replace does not accept spaces? timdvtemp Linux - Software 5 02-16-2011 05:19 PM
how do i replace a text string in a file with a random string? (with sed etc) steve51184 Linux - Software 16 09-02-2010 12:05 PM
Sed - Replace all spaces at beginning of line with the number 1. felix001 Programming 12 06-21-2010 01:22 PM
sed replace string octeto Programming 4 06-06-2007 03:09 AM


All times are GMT -5. The time now is 08:26 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration