Sed escapando "." y usar $variable?

Sed escapando "." y usar $variable?

No entiendo esto de escapar. ¿Hay algún generador disponible para ello?

Necesito reemplazar el texto normal (185...) con la variable $NEW_DNS

sed -i "185.228.168.168,185.228.169.168|$NEW_DNS /etc/wireguard/wg0.conf

Producción

sed: -e expression #1, char 4: unknown command: `.'

Actualización: me gusta que puedas ver las respuestas y su trabajo, solo que /etc/wireguard/wg0.conf no se reescribe con datos nuevos (pero la salida es correcta.

# client start menu
echo "What can i do for you today? "
options=("Show Clients" "Show your DNS Server" "Change your DNS Server" "Install/add new Client" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "Show Clients")
            ls *.conf
            ;;
            "Show your DNS Server")
            if [ $( head -n1 $CONF | awk '{print $5}') == "1.1.1.1,1.0.0.1" ]; then
                echo "Your Public DNS Server is Cloudflare (1.1.1.1,1.0.0.1), great unfiltered choice for best speed worldwide! "
            fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "176.103.130.130,176.103.130.131" ]; then
                echo "Your Public DNS Server is Adguard (176.103.130.130,176.103.130.131), Advertising Filter kill them all! "
            fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "84.200.69.80,84.200.70.40" ]; then
                echo "Your Public DNS Server is WATCH.DNS (84.200.69.80,84.200.70.40), great unfiltered choice for German Clients! "
            fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "9.9.9.9, 149.112.112.112" ]; then
                echo "Your Public DNS Server is QUAD9 (9.9.9.9, 149.112.112.112), great TLS encrypted unfiltered choice! "
            fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "77.88.8.7,77.88.8.3" ]; then
                echo "Your Public DNS Server is Yandex (77.88.8.7,77.88.8.3), safe choice with Family Filter! "
            fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "185.228.168.168,185.228.169.168" ]; then
                echo "Your Public DNS Server is Clean Browsing (185.228.168.168,185.228.169.168), Uuh TLS encrypted safe choice with Family Filter and Youtube-Safe Option! "
                fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "10.8.0.1" ]; then
                echo "Nothing else then own encrypted and logless dedicated DNS Server! "
                 fi
                 ;;
            "Change your DNS Server")
             if [ $( head -n1 $CONF | awk '{print $5}') == "1.1.1.1,1.0.0.1" ]; then
                 echo "Your Public DNS Server is Cloudflare (1.1.1.1,1.0.0.1), great unfiltered choice for best speed worldwide! "
             fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "176.103.130.130,176.103.130.131" ]; then
                 echo "Your Public DNS Server is Adguard (176.103.130.130,176.103.130.131), Advertising Filter kill them all! "
             fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "84.200.69.80,84.200.70.40" ]; then
                 echo "Your Public DNS Server is WATCH.DNS (84.200.69.80,84.200.70.40), great unfiltered choice for German Clients! "
             fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "9.9.9.9, 149.112.112.112" ]; then
                 echo "Your Public DNS Server is QUAD9 (9.9.9.9, 149.112.112.112), great TLS encrypted unfiltered choice! "
             fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "77.88.8.7,77.88.8.3" ]; then
                 echo "Your Public DNS Server is Yandex (77.88.8.7,77.88.8.3), safe choice with Family Filter! "
             fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "185.228.168.168,185.228.169.168" ]; then
                      echo "Your want leave your Clean Browsing server and use another one?. You need generate new configs later, if you want to use new Server (Start Menu choice: 3 ) "
                      # client choice NEW DNS
                          read -rp "Do you really want to change your DNS Server? (y/n) " -e -i y NEW_DNS
                            if [ "$NEW_DNS" == "y" ]; then
                              echo "Which DNS do you want to use with the VPN?"
                                echo "You recognise encrypted DNS with "TLS" (Port 853 can be potentially blocked through Government etc.) Some Servers use logs, but no one of them log aWireguard IP. "
                              echo "   1) Cloudflare: log: yes 24h, Filter: no, + best speed worldwide"
                              echo "   2) AdGuard: Log: yes, Filter: advertising"
                              echo "   3) DNS.WATCH: Log: no, Filter: no, + great speed for Germany"
                              echo "   4) Quad9: TLS: yes, Log: yes, Filter: no"
                              echo "   5) Yandex Family: TLS:no, Log: yes, Filter: adult"
                              echo "   6) Clean Browsing Family: TLS: yes, Log: yes, Filter: adult and explicit sites, Youtube- safe mode"
                              read -p "DNS [1-6]: " -e -i 1 NEW_DNS

                              case $NEW_DNS in
                                  1)
                                      NEW_DNS="1.1.1.1,1.0.0.1"
                                  ;;
                                  2)
                                  NEW_DNS="176.103.130.130,176.103.130.131"
                                  ;;
                                  3)
                                  NEW_DNS="84.200.69.80,84.200.70.40"
                                  ;;
                                  4)
                                  NEW_DNS="9.9.9.9, 149.112.112.112"
                                  ;;
                                  5)
                                  NEW_DNS="77.88.8.7,77.88.8.3"
                                  ;;
                                  6)
                                  NEW_DNS="185.228.168.168,185.228.169.168"
                                  ;;
                              esac
                            fi
                      # EOF client choices DNS
                fi
                sed "s/185\.228\.168\.168,185\.228\.169\.168/$NEW_DNS/" /etc/wireguard/wg0.conf
               if [ $( head -n1 $CONF | awk '{print $5}') == "10.8.0.1" ]; then
                 echo "Nothing else then own encrypted and logless dedicated DNS Server! "
                 fi
                 ;;
        "Install/add new Client")
            break
            ;;
        "Quit")
              exit
            ;;
    esac
done

Respuesta1

Si desea reemplazar esa cadena con lo que tiene en la variable:

sed "s|185\.228\.168\.168,185\.228\.169\.168|$NEW_DNS|" /etc/wireguard/wg0.conf

Necesitas sed s|string|replacement|. Puede utilizarlo sed s|string|replacement|gpara reemplazarlo en todo el archivo. También debes escapar .con una barra invertida para que no represente un solo carácter. Quité el -iinterruptor para que los cambios se escriban en la salida estándar para que puedas asegurarte de que es lo que deseas. Puede volver a colocarlo en el comando una vez que haya confirmado que así es.

Si desea editar el archivo en sí, agregue el -imodificador a su comando.

sed -i "s|185\.228\.168\.168,185\.228\.169\.168|$NEW_DNS|" /etc/wireguard/wg0.conf

Respuesta2

El scomando en sedrealiza una sustitución de cualquier cosa que coincida con la expresión regular dada con el texto de reemplazo dado. Su sintaxis es s/expression/text/flags.

En tu caso, quieres usar la expresión

185\.228\.168\.168,185\.228\.169\.168

He escapado de los puntos porque son "especiales" en las expresiones regulares (un punto sin escape coincide con cualquier carácter, pero usted desea que coincida con los puntos reales).

El texto que desea utilizar como texto de reemplazo está en una variable. Esto significa que toda la sedexpresión tiene que ser una cadena entre comillas dobles (ya que el shell ignoraría la variable en una cadena entre comillas simples).

Además, no necesita las banderas opcionales con el scomando aquí, por lo que obtenemos

sed "s/185\.228\.168\.168,185\.228\.169\.168/$NEW_DNS/" filename

No utilices sed -ipara realizar una edición in situ hasta que puedas comprobar por ti mismo que se obtuvo el resultado correcto.

Tenga en cuenta también que, dado que estamos inyectando el valor de su variable en el sedscript, esto fallaría si $NEW_DNScontuviera algún /carácter (lo cual creo que sería poco probable que ocurra en este caso particular).

Si por coma quiere decir "esta o aquella dirección", tendrá que utilizar una alternancia (con |):

sed -E "s/185\.228\.168\.168|185\.228\.169\.168/$NEW_DNS/" filename

Lo estoy usando sed -Eaquí porque necesitamos habilitar expresiones regulares extendidas ( |no forma parte de la sintaxis básica de expresiones regulares, que es la predeterminada).

información relacionada