Greg
Greg

Reputation: 257

Awk outputting '00' instead of proper format

I am trying to print output to an HTML file from another file using bash. The file I am trying to extract from looks like this:

192.168.1.42    42.8G    45.1G
192.168.1.47    47.2G    35.8G

What I am trying to do is write it to a table with a "More Info" button which will be a link. Here is the "awk" command I am trying to use:

awk '{print "      <tr><td>"$1"</td><td>"$2"</td><td>"$3"</td><td><a href=#"$1"><button class="'"btn btn-primary btn-sm"'">More Info</button></a></td></tr>"}'

Output to the HTML document I am looking for is:

<tr><td>192.168.1.42</td><td>42.8G</td><td>45.1G</td><td><a href=#192.168.1.42><button class="btn btn-primary btn-sm">More Info</button></a></td></tr>

The main issue is the "Button" class

Is there anyway I can do this or a better way in bash to do this?

Upvotes: 0

Views: 48

Answers (1)

fedorqui
fedorqui

Reputation: 289835

You are getting 00 because it is the result of btn-primary and btn-sm. You probably want to escape the " so that the text gets printed properly:

<button class=\"btn btn-primary btn-sm\">More Info</button>
              ^                       ^

Your current

<button class="'"btn btn-primary btn-sm"'">More Info</button>

tries to use the variables btn, primary and sm, which are not defined, and prints the result of btn - primary and btn - sm, which is 0 as per variables not defined.

See more generally how awk behaves when operating on undefined variables:

$ awk 'BEGIN{print a-b}'
0

Finally, printf could help you handling values in a better way:

awk '{printf "<tr><td>%d</td><td>%d</td></tr>\n", $1, $2}' file

Upvotes: 5

Related Questions