LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need help with sed (https://www.linuxquestions.org/questions/linux-newbie-8/need-help-with-sed-4175541736/)

pump1t 05-05-2015 11:29 PM

Need help with sed
 
I have config below:
location /c21p/ {
proxy_pass http://acq_c21connect/acq_c21p/; #DIRECTS TO Prod instance
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_redirect default;

I wanted to search for 'location /c21p/' and if found add this line "proxy_buffering off;" at the end of the block, so the output will be:

location /c21p/
proxy_pass http://acq_c21connect/acq_c21p/; #DIRECTS TO Prod instance
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_redirect default;
proxy_buffering off;

tried using this sed command, but didnt work:
sed -e "/'location /c21p/' {N;N;N;s/$/\'proxy_buffering off';}" acq_chris21.com.conf > acq.txt

grail 05-06-2015 12:56 AM

So what you have show is the entire file?
Does the added line need to be after 'proxy_redirect default;'?
Is there a closing curly brace? ie } to match the opening one on the location line?

Please explain what the following is meant to do?
Code:

/'location /c21p/'
I ask as there are no single quotes in your example file and // tells sed to look for what is between them yet you have ///, which I would think would confuse sed??

Please also use [code][/code] tags around code and data to make it clearer and easier to read.

syg00 05-06-2015 01:08 AM

Note also the opening curly has disappeared in the (wanted) output.
Very confusing before even attempting to parse the sed.

Personally I'd probably use awk - if I knew what was really wanted.

pump1t 05-08-2015 12:23 AM

this didn't help, what I wanted to happen is to search for:

"location /c21p/" in the input file and add "proxy_buffering off;" to the last entry before the "}", eg:

output will be:

location /c21p/ {

....
....
....
proxy_buffering off;
}

pan64 05-08-2015 12:57 AM

you need to use \ here and do not need '
Code:

# instead of:
sed -e "/'location /c21p/'
#
sed -e "/location \/c21p/

please do not reply "this didn't help", this is not the full solution. I suggest you to use awk too.

grail 05-08-2015 04:34 AM

In fairness to syg00 and myself, we are trying to help, but the poor quality of the request made that difficult so we were trying to get more information from you to help us to help you.

As has been suggested, and now aided with your additional information, awk would be better suited as you can provide the record separator (RS) to be your curly braces.
Now this would initially assume that there are no additional curly braces found inside, ie. could you have:
Code:

location /c21p/ {
....
..some text{ some more } ..
....
}

If above is a possible scenario you would need to be cautious building your regex for the RS argument.

As you may now see, what seems crystal clear to you may illicit more questions from those of us who are not looking at what you are and if you do not paint an appropriate picture we may provide
information that is of no help.

To finish, are you familiar with awk and can go from here?

awk manual


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