Skip to main content
deleted 66 characters in body
Source Link
Michael Mrozek
  • 95.7k
  • 40
  • 245
  • 236

What I got working (edited per StephenHarris comment) is:

domain=aa ; sed s"/\$domain/$domain/" ~/nginx_app >> ${domain}.conf

Let me know. If it works for you, mark the answer as accepted.

The second $domain of the sed command expands into aa in my test case. However, since the first $domain has its $ identifier escaped, it is NOT expanded.

There exists another command-line program called awk which has a more powerful way of passing variables from outside it to inside it, but sed is considered faster and more efficient (not sure if really true nowadays), so that's why I answered with sed because this was a simple case. In awk, you could use:

awk -v domain=aa '{gsub("\\$domain",domain);print}' ~/nginx_app >> ${domain}.conf

Here, the -v option to awk defines an awk variable, and gsub is an awk function for performing a global substitution for a given line. The escapinng sequence \\ is slightly longer than for the sed solution.

What I got working (edited per StephenHarris comment) is:

domain=aa ; sed s"/\$domain/$domain/" ~/nginx_app >> ${domain}.conf

Let me know. If it works for you, mark the answer as accepted.

The second $domain of the sed command expands into aa in my test case. However, since the first $domain has its $ identifier escaped, it is NOT expanded.

There exists another command-line program called awk which has a more powerful way of passing variables from outside it to inside it, but sed is considered faster and more efficient (not sure if really true nowadays), so that's why I answered with sed because this was a simple case. In awk, you could use:

awk -v domain=aa '{gsub("\\$domain",domain);print}' ~/nginx_app >> ${domain}.conf

Here, the -v option to awk defines an awk variable, and gsub is an awk function for performing a global substitution for a given line. The escapinng sequence \\ is slightly longer than for the sed solution.

What I got working (edited per StephenHarris comment) is:

domain=aa ; sed s"/\$domain/$domain/" ~/nginx_app >> ${domain}.conf

The second $domain of the sed command expands into aa in my test case. However, since the first $domain has its $ identifier escaped, it is NOT expanded.

There exists another command-line program called awk which has a more powerful way of passing variables from outside it to inside it, but sed is considered faster and more efficient (not sure if really true nowadays), so that's why I answered with sed because this was a simple case. In awk, you could use:

awk -v domain=aa '{gsub("\\$domain",domain);print}' ~/nginx_app >> ${domain}.conf

Here, the -v option to awk defines an awk variable, and gsub is an awk function for performing a global substitution for a given line. The escapinng sequence \\ is slightly longer than for the sed solution.

Incorporate StephenHarris' correction comment
Source Link
user1404316
  • 3.1k
  • 15
  • 23

What I got working (edited per StephenHarris comment) is:

domain=aa ; eval sed s"/\\\$domain\$domain/$domain/" ~/nginx_app >> ${domain}.conf

Let me know. If it works for you, mark the answer as accepted.

What is happening is that eval is a bash built-in command that evaluates all its arguments and then evaluates them as a command. Thus, theThe second $domain of the sed command expands into aa in my test case. However, since the first $domain has its $ identifier escaped (three required within sed - would take a longer explanation, if you need, ask) it is NOT expanded.

There exists another command-line program called awk which has a more powerful, direct, and prettier way of passing a variablevariables from outside it to inside it, but sed is considered faster anand more efficient (not sure if really true nowadays), so that's why I answered with sed because this was a simple case. In awk, you could use:

awk -v domain=aa '{gsub("\\$domain",domain);print}' ~/nginx_app >> ${domain}.conf

Here, the -v option to awk defines an awk variable, and gsub is an awk function for performing a global substitution for a given line. The escapinng sequence \\ is slightly prettierlonger than for the sed solution.

What I got working is:

domain=aa ; eval sed s"/\\\$domain/$domain/" ~/nginx_app >> ${domain}.conf

Let me know. If it works for you, mark the answer as accepted.

What is happening is that eval is a bash built-in command that evaluates all its arguments and then evaluates them as a command. Thus, the second $domain of the sed command expands into aa in my test case. However, since the first $domain has its $ identifier escaped (three required within sed - would take a longer explanation, if you need, ask) it is NOT expanded.

There exists another command-line program called awk which has a more powerful, direct, and prettier way of passing a variable from outside it to inside it, but sed is considered faster an more efficient (not sure if really true nowadays), so that's why I answered with sed because this was a simple case. In awk, you could use:

awk -v domain=aa '{gsub("\\$domain",domain);print}' ~/nginx_app >> ${domain}.conf

Here, the -v option to awk defines an awk variable, and gsub is an awk function for performing a global substitution for a given line. The escapinng sequence \\ is slightly prettier than for the sed solution.

What I got working (edited per StephenHarris comment) is:

domain=aa ; sed s"/\$domain/$domain/" ~/nginx_app >> ${domain}.conf

Let me know. If it works for you, mark the answer as accepted.

The second $domain of the sed command expands into aa in my test case. However, since the first $domain has its $ identifier escaped, it is NOT expanded.

There exists another command-line program called awk which has a more powerful way of passing variables from outside it to inside it, but sed is considered faster and more efficient (not sure if really true nowadays), so that's why I answered with sed because this was a simple case. In awk, you could use:

awk -v domain=aa '{gsub("\\$domain",domain);print}' ~/nginx_app >> ${domain}.conf

Here, the -v option to awk defines an awk variable, and gsub is an awk function for performing a global substitution for a given line. The escapinng sequence \\ is slightly longer than for the sed solution.

added 226 characters in body
Source Link
user1404316
  • 3.1k
  • 15
  • 23

What I got working is:

domain=aa ; eval sed s"/\\\$domain/$domain/" ~/nginx_app >> ${domain}.conf

Let me know. If it works for you, mark the answer as accepted.

What is happening is that eval is a bash built-in command that evaluates all its arguments and then evaluates them as a command. Thus, the second $domain of the sed command expands into aa in my test case. However, since the first $domain has its $ identifier escaped (three required within sed - would take a longer explanation, if you need, ask) it is NOT expanded.

There exists another command-line program called awk which has a more powerful, direct, and prettier way of passing a variable from outside it to inside it, but sed is considered faster an more efficient (not sure if really true nowadays), so that's why I answered with sed because this was a simple case. In awk, you could use:

awk -v domain=aa '{gsub("\\$domain",domain);print}' ~/nginx_app >> ${domain}.conf

Here, the -v option to awk defines an awk variable, and gsub is an awk function for performing a global substitution for a given line. The escapinng sequence \\ is slightly prettier than for the sed solution.

What I got working is:

domain=aa ; eval sed s"/\\\$domain/$domain/" ~/nginx_app >> ${domain}.conf

Let me know. If it works for you, mark the answer as accepted.

What is happening is that eval is a bash built-in command that evaluates all its arguments and then evaluates them as a command. Thus, the second $domain of the sed command expands into aa in my test case. However, since the first $domain has its $ identifier escaped (three required within sed - would take a longer explanation, if you need, ask) it is NOT expanded.

There exists another command-line program called awk which has a more powerful, direct, and prettier way of passing a variable from outside it to inside it, but sed is considered faster an more efficient (not sure if really true nowadays), so that's why I answered with sed because this was a simple case. In awk, you could use:

awk -v domain=aa '{gsub("\\$domain",domain);print}' ~/nginx_app >> ${domain}.conf

What I got working is:

domain=aa ; eval sed s"/\\\$domain/$domain/" ~/nginx_app >> ${domain}.conf

Let me know. If it works for you, mark the answer as accepted.

What is happening is that eval is a bash built-in command that evaluates all its arguments and then evaluates them as a command. Thus, the second $domain of the sed command expands into aa in my test case. However, since the first $domain has its $ identifier escaped (three required within sed - would take a longer explanation, if you need, ask) it is NOT expanded.

There exists another command-line program called awk which has a more powerful, direct, and prettier way of passing a variable from outside it to inside it, but sed is considered faster an more efficient (not sure if really true nowadays), so that's why I answered with sed because this was a simple case. In awk, you could use:

awk -v domain=aa '{gsub("\\$domain",domain);print}' ~/nginx_app >> ${domain}.conf

Here, the -v option to awk defines an awk variable, and gsub is an awk function for performing a global substitution for a given line. The escapinng sequence \\ is slightly prettier than for the sed solution.

add awk solution
Source Link
user1404316
  • 3.1k
  • 15
  • 23
Loading
Source Link
user1404316
  • 3.1k
  • 15
  • 23
Loading