How to escape {} in awk?

0

Issue

I have many of the input lines as seen below, where I would like to end up with an output similar to this

host tsclient010.tsclient {
   option host-name "tsclient010.tsclient";
   hardware ethernet 00E0C56BF96D;
   fixed-address 192.168.246.10;
}

fore ach of the inputs. However I run into the problem that the static content contains {}.

echo "192.168.246.10 00E0C56BF96D tsclient010.tsclient" | awk '{print
host $3 {
   option host-name "$3";
   hardware ethernet $2;
   fixed-address $1;
}
}'

Output:

awk: cmd. line:2:     host $3 {
awk: cmd. line:2:             ^ syntax error

I have tried to escape it by '\{', but doesn’t work.

Question

Does anyone know how to escape the needed chars?

Solution

  1. Text inside double quotes is text. Text not inside double quotes is awk script.
  2. The newline delimeters awk commands.

You can:

echo "192.168.246.10 00E0C56BF96D tsclient010.tsclient" | awk '{
    print "host "$3" {"
    print "option host-name \""$3"\";"
    print "hardware ethernet "$2";"
    print "fixed-address "$1";"
    print "}"
}'

or

echo "192.168.246.10 00E0C56BF96D tsclient010.tsclient" | awk '{
    print "host "$3" {\noption host-name \""$3"\";\nhardware ethernet "$2";\nfixed-address "$1";\n}" }'

Answered By – KamilCuk

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More