0

I have been told that I can use grep & sed to find and change text in a file, but I do not understand enough to make it work for myself.

I have a configuration file (lighttpd.conf) that I need to add a new line into:

server.modules  += (
        "mod_setenv"
)
setenv.add-response-header = ( "X-UA-Compatible" => "IE=edge",
                               "X-Frame-Options" => "Deny" )
setenv.add-environment = ("LC_ALL" => "en_US.UTF-8")
$HTTP["url"] =~ "config.js$" {
        setenv.add-response-header = ( "Cache-Control" => "no-store, no-cache, must-revalidate, post-check=0, pre-check=0" )
}

ssl.cipher-list = "!aNULL:kECDH+AESGCM:ECDH+AESGCM:RSA+AESGCM:kECDH+AES:ECDH+AES:RSA+AES"
ssl.dh-file = "/etc/lighttpd/dhparams.pem"

$HTTP["url"] =~ "^/restapi" {
    proxy.server = ( "" =>
                    ((
                      "host" => "127.0.0.1",
                      "port" => "9202"
                    ))
                   )
}

url.rewrite-if-not-file = ( "^/ui(?!\.\w+$)" => "/index.html",
"^/login" => "/index.html" )

server.stream-response-body = 2

#------HTTPPORT_CONFIG START------
#------HTTPPORT_CONFIG END------

I need to add the line ssl.ca-file = "/etc/lighttpd/ca-cert.pem" after the line ssl.dh-file = "/etc/lighttpd/dhparams.pem" like this:

ssl.cipher-list = "!aNULL:kECDH+AESGCM:ECDH+AESGCM:RSA+AESGCM:kECDH+AES:ECDH+AES:RSA+AES"
ssl.dh-file = "/etc/lighttpd/dhparams.pem"
ssl.ca-file = "/etc/lighttpd/ca-cert.pem"

$HTTP["url"] =~ "^/restapi" {
    proxy.server = ( "" =>

I have spent a few hours trying to understand RegEx but I cannot even work out the string needed to to locate the ssl.dh-file line, let alone how to insert a new line after it.

I would be very grateful if someone can help me.

3
  • Can you assume it's not already there? Or are you OK if it's added twice?
    – Jeff Schaller
    Jan 13 '20 at 12:51
  • Is this a one-off or a recurring action? For a one-off - use an interactive editor.
    – RudiC
    Jan 13 '20 at 12:58
  • Also, what operating system are you using? Can we assume GNU tools (default on Linux)?
    – terdon
    Jan 13 '20 at 13:10
1

The only tricky part I see is that your pattern and replacement strings contain forward slashes - so to avoid having to escape them, you'll want to set a different delimiter such as % for sed ex.

sed '
\%^ssl.dh-file = "/etc/lighttpd/dhparams.pem"%a\
ssl.ca-file = "/etc/lighttpd/ca-cert.pem"
' lighttpd.conf
1
  • Thank you so much, this is perfect. I doubt I would have ever worked that out for myself. I really appreciate your help. Pin.
    – Pin Pin
    Jan 13 '20 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.