Sed remove character at beginning of string

Multi tool use
Multi tool use


Sed remove character at beginning of string



I have the following script which inserts a "#" before a IP-address in a nginx config to act as a comment.


#!/bin/bash
hosts=( 84.19.155.71 84.19.155.72 )
for i in "${hosts[@]}"
do
CHECK=`grep $i nginx.conf`
if [[ ! "$CHECK" =~ ^#.*$ ]]
then
nc -zw 5 $i 5044
if [ "$?" != "0" ]
then
sed -i "/$i/s/^/#/" nginx.conf
else
sed -i 's/#*$i/$i/' nginx.conf
fi
fi
done



It works to insert the "#" at the beginning of the string but now I want to remove it if the exit code of the nc command is 0. I tried it with my else statement as shown above but to no avail. This is what the nginx.conf looks like:


nc


nginx.conf


user root;
worker_processes auto;
pid /run/nginx.pid;
load_module /usr/lib/nginx/modules/ngx_stream_module.so;


worker_rlimit_nofile 1000000;

events {
worker_connections 20000;
# multi_accept on;
}

stream {
log_format basic '$time_iso8601 $remote_addr '
'$protocol $status $bytes_sent $bytes_received '
'$session_time $upstream_addr '
'"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';

# Enable access_log statements for debugging
access_log /var/log/nginx/stream.log basic;

upstream syslog_udp {
#hash $remote_addr;
server 84.19.155.71:514 max_fails=2 fail_timeout=5s;
# server 84.19.155.72:514 max_fails=2 fail_timeout=5s;
}
server {
listen 514 udp;
proxy_pass syslog_udp;
proxy_responses 0;
#proxy_timeout 2s;
proxy_bind $remote_addr transparent;
#proxy_next_upstream_timeout 2s;
}
}



Can anyone help removing the "#" character from the IP-address?




2 Answers
2



First, only lines that do not begin with # are processed by the first if statement if [[ ! "$CHECK" =~ ^#.*$ ]]


if [[ ! "$CHECK" =~ ^#.*$ ]]



Adding # looks fine and for removing, you could try:


sed -i "s/^#*$i//" nginx.conf





Yes this solved it, thank you very much!
– nillenilsson
Jul 3 at 7:38



if [[ ! "$CHECK" =~ ^#.*$ ]] this will take only lines which doesnt start with "#"
you may have to revisit that part which i will not discuss.



But here is the sed replace which might help you.


user@machine:$cat sample
server 84.19.155.71:514 max_fails=2 fail_timeout=5s;
#server 84.19.155.72:514 max_fails=2 fail_timeout=5s;

user@machine:$sed "s/^#(.*$i.*)/1/g" sample
server 84.19.155.71:514 max_fails=2 fail_timeout=5s;
server 84.19.155.72:514 max_fails=2 fail_timeout=5s;





This works just as well as the solution above, Thank you very much!
– nillenilsson
Jul 3 at 7:39






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

ygqc40anbkdHBNV odvKLG
NAzEPYaVa5wepC 2,CvfsG3BQa8egT pnAyj,Udw1cmAyBhD19 Ik 7I jytVsNOduzwAI5j,1v,O0q7KM uWeKWrgPwD3fE6xk3tZ,xK,B

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications