2

I have a variable with dollar signs in it:

ipvar_definitions="
var SSH_SERVERS \$HOME_NET
var FTP_SERVERS \$HOME_NET
var SIP_SERVERS \$HOME_NET
"

I want to append the value of this variable after a match using sed or perl or whatever one liner I can use.

When trying with sed using the code below:

sed   "/var SNMP_SERVERS/a ${ipvar_definitions}" /etc/snort.conf

I get this error:

sed: -e expression #1, char 24: expected newer version of sed

I get this error because my variable has dollar signs in it. I'd like to solve this in a one liner using sed, perl, awk or whatever I can use.

For clarification I am posting a sample input and the expected output:

Input:

# List of snmp servers on your network
var SNMP_SERVERS $HOME_NET

# Configure your service ports.  This allows snort to look for attacks destined

Expected output:

# List of snmp servers on your network
var SNMP_SERVERS $HOME_NET
var SSH_SERVERS $HOME_NET
var FTP_SERVERS $HOME_NET
var SIP_SERVERS $HOME_NET

# Configure your service ports.  This allows snort to look for attacks destined
1
  • The error probably comes from your sed version requiring a backslash before each literal newline you want to insert. Commented Dec 19, 2017 at 5:59

2 Answers 2

2

Using gnu awk you can do this:

# no need to escape the $ if you use single quotes
ipvar_definitions='var SSH_SERVERS $HOME_NET
var FTP_SERVERS $HOME_NET
var SIP_SERVERS $HOME_NET'

# use awk command to insert ipvar_definitions after a line containing "var SNMP_SERVERS"
awk -v var="$ipvar_definitions" '1; /var SNMP_SERVERS/{print var}' /etc/snort.conf

Output:

# List of snmp servers on your network
var SNMP_SERVERS $HOME_NET
var SSH_SERVERS $HOME_NET
var FTP_SERVERS $HOME_NET
var SIP_SERVERS $HOME_NET

# Configure your service ports.  This allows snort to look for attacks destined
Sign up to request clarification or add additional context in comments.

4 Comments

To save output of this awk command in place use: awk -i inplace -v var="$ipvar_definitions" '1; /var SNMP_SERVERS/{print var}' /etc/snort.conf
There's nothing gawk-specific about that command. It will become so if you add -i inplace of course but the OP doesn't seem to be asking for that.
I mentioned gnu awk because my OSX awk didn't like multiline variable i.e -v var="$ipvar_definitions"
Really? That's bizarre. I wonder if that's an OSX shell problem or an awk problem. I just tested with the default awk and GNU awk on OSX and it's an OSX awk problem as GNU awk works fine with it so the shell isn't having a problem with it. Weird...
0
$ export ipvar_definitions='
var SSH_SERVERS $HOME_NET
var FTP_SERVERS $HOME_NET
var SIP_SERVERS $HOME_NET
'

$ perl -pe 's/.*var SNMP_SERVERS.*/$&$ENV{ipvar_definitions}/' /etc/snort.conf 
# List of snmp servers on your network
var SNMP_SERVERS $HOME_NET
var SSH_SERVERS $HOME_NET
var FTP_SERVERS $HOME_NET
var SIP_SERVERS $HOME_NET


# Configure your service ports.  This allows snort to look for attacks destined

Further reading: https://perldoc.perl.org/Env.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.