using sed command need to change one line in nginx conf - sed

Need to change one line in nginx.conf
client_max_body_size 1m to client_max_body_size 10m I used this command
sed -i "s/^client_max_body_size 1m;$/client_max_body_size 10m;/g" /etc/nginx/nginx.conf
sed: 1: "nginx.conf": extra characters at the end of n command
got this message... I don't know what I did wrong.

You can use
sed -i '' 's/^client_max_body_size 1m;$/client_max_body_size 10m;/g' /etc/nginx/nginx.conf
The '' after -i option in FreeBSD sed enable inplace matching.

Related

SED inplace file change inside make - How?

sed inplace change on a file is not working inside Make object.
I want to replace a line in a file with sed called in a make object. But it does not seem to be working. How can I fix this?
change_generics:
ifeq ($(run_TESTNAME), diagnostics)
ifeq ($(run_TESTCASE), 1)
sed -i -e "s/SIM_MULTI\==[a-z,A-Z]*/SIM_MULTI=TRUE/" ./generics.f
else ifeq ($(TESTCASE), 2)
sed -i -e "s/SIM_MISSED\==[a-z,A-Z]*/SIM_MISSED=TRUE/" ./generics.f
endif
endif
I would like the generics.f file changed with that one line change. But it remains the same as the original. The sed command works outside make.
I can't reproduce this using GNU sed 4.2.2 and GNU make 3.82, or at least, I can't reproduce any scenario where the same sed command works from the command line but not in a Makefile.
Simpler Makefile:
all:
# Contrived just so I can test your 2 sed commands.
sed -i -e "s/SIM_MULTI\==[a-z,A-Z]*/SIM_MULTI=TRUE/" ./generics.f
sed -i -e "s/SIM_MISSED\==[a-z,A-Z]*/SIM_MISSED=TRUE/" ./generics.f
Sample file content in generics.f:
SIM_MULTI=foo
SIM_MISSED=bar
Testing:
$ make all
sed -i -e "s/SIM_MULTI\==[a-z,A-Z]*/SIM_MULTI=TRUE/" ./generics.f
sed -i -e "s/SIM_MISSED\==[a-z,A-Z]*/SIM_MISSED=TRUE/" ./generics.f
Confirmed that both sed commands fail to edit a file with this content.
To fix:
Probably, you need to simply remove the \= from your regular expression. The backslash there has no effect, and causes your regex to simply match two equals signs ==. Thus this works:
all:
sed -i 's/SIM_MULTI=[a-zA-Z]*/SIM_MULTI=TRUE/' ./generics.f
sed -i 's/SIM_MISSED=[a-zA-Z]*/SIM_MISSED=TRUE/' ./generics.f
Testing:
$ make all
sed -i 's/SIM_MULTI=[a-zA-Z]*/SIM_MULTI=TRUE/' ./generics.f
sed -i 's/SIM_MISSED=[a-zA-Z]*/SIM_MISSED=TRUE/' ./generics.f
$ cat generics.f
SIM_MULTI=TRUE
SIM_MISSED=TRUE
Further explanation:
There is no need to specify -e there.
There is no need to enclose the script in double quotes, which is riskier because it allows the contents to be modified by the shell.
The bug appears to be \= and I deleted those characters, as mentioned above.
Note that I removed the comma , as well in [a-z,A-Z]. I think that probably isn't what you meant, and it would cause a class of characters including a-z, A-Z and a comma , to be matched by the regex. (And if it is what you mean, you might consider writing it as [a-zA-Z,] as that would be less confusing.)
If this has not resolved your issue, I would need to know things like:
What is the version of your sed.
What is the contents in generics.f.
POSIX/GNU sed have c for "change":
sed -i '/SIM_MULTI=/c\SIM_MULTI=TRUE'
sed -i '/SIM_MISSED=/c\SIM_MISSED=TRUE'

deleting all private IP addresses

Currently I am working on a project where I have a file full of IP addresses I want to delete the IP adddresses that are private (whole range) and using bash to accomplish it. I do make a match of private addresses but unable to delete it.
I have tried:
sed -i '/(192)\.(168)(\.([2][0-5][0-5]|[1][0-9][0-9]|[1-9][0-9]|[0-9])){2}/d' validIPOnly
same for
127.x.x.x,172.16.x.X,10.x.x.x
When using capture groups with sed you either need to escape the ( ) or specify option -E:
$ sed -E -i '/(...)...'
or
$ sed -i '/\(...\)...`
For GNU sed only use the -r option instead of -E.

Modify /etc/sudoers with sed

I'm trying to write a sed program to append Defaults:user !requiretty after the line Defaults requiretty in /etc/sudoers. I tried the following command:
sudo sed -i '/Defaults requiretty/a Defaults:user !requiretty' /etc/sudoers
This is working properly, but only if there are 4 spaces between 'Defaults' and 'requiretty'. I want to modify it in order to work with any number of spaces, so I tried the following:
sudo sed -i '/Defaults\s+requiretty/a Defaults:user !requiretty' /etc/sudoers
I checked the pattern on regexr and it was okay, but still the command does not insert the required line. Why not?
try this;
sed '/Defaults.\s\s.requiretty/a Defaults:user !requiretty' /etc/sudoers
Working from Mustafa's answer, here's a way to do the same thing with some safety checks added
SUDOER_TMP=$(mktemp)
cat /etc/sudoers > $SUDOER_TMP
sed -i -e 's/PATTERN/OUTPUT/' $SUDOER_TMP
visudo -c -f $SUDOER_TMP && \ # this will fail if the syntax is incorrect
cat $SUDOER_TMP > /etc/sudoers
rm $SUDOER_TMP

Sed remove matching lines script

I'm requesting help with a very simple script...
#!/usr/bin/sed -f
sed '/11,yahoo/d'
sed '/2506,stackover flow/d'
sed '/2536,reddit/d'
Just need it to remove three matches that account for 18408 in my file, data.csv
% sed -f remove.sed < data.csv
sed: 3: remove.sed: unterminated substitute pattern
Doing these same lines individually is no problem at all, so what am I doing wrong with this?
Using freeBSD 10.1 and its implementation of sed, if that matters.
This, being a sed script, should not have "sed" at each line.
Either change it to:
#!/usr/bin/sed -f
/11,yahoo/d
/2506,stackover flow/d
/2536,reddit/d
Or to
#!/bin/sh
sed -e /11,yahoo/d \
-e /2506,stackover flow/d \
-e /2536,reddit/d

restricting sed command

I have a variables that needs to be modified so that minutes %M is added. I know the sed command and it is working as expected.
# cat mysed.txt
myfirstfile="comany$mydb`date +'%d-%b-%Y-%H'`.sql"
# sed -i 's/\%H/\%H-\%M/' mysed.txt
# cat mysed.txt
myfirstfile="company$mydb`date +'%d-%b-%Y-%H-%M'`.sql"
But if I run the same sed command again, it will add %M again as follows.
# sed -i 's/\%H/\%H-\%M/' mysed.txt
# cat mysed.txt
myfirstfile="company$mydb`date +'%d-%b-%Y-%H-%M-%M'`.sql"
I need a sed command that should add the minutes %M only once even if sed command is executed twice (by mistake)
# sed -i "s/%H'/%H-%M'/" mysed.txt
This should work. This way it will only do the replacement if there is a quote mark next to the %H.
Test if it is present before substituting:
sed '/%H-%M/! s/%H/%H-%M/' mysed.txt
Btw. % does not need to be escaped.
I believe below command will work fine.Please replace test.dat file with your file name.
cat test.dat|sed "s/\%H\'/\%H\-\%M\'/" > test.dat
Without cat command
sed -i "s/\%H\'/\%H\-\%M\'/" test.dat > test.dat
Cheers!