Home > Back-end >  awk not printing output
awk not printing output

Time:06-14

I am getting this output

grep -hnr '$pattern' /path/of/file/ | awk -F '=' 'BEGIN { print "<table border='1'><tr>" } { print "<td>" $3 "</td>" } { print "</tr>" }'`;

the above code is not printing anything when awk command is added to it. Here I'm trying to convert the file data into table format using awk command.

Example of data in the files:

name==ABC
City==Rohini
State==Delhi
Age==18
Country==India

Expected output:

<html><table border="1">
<tr><td>ABC</td></tr>
<tr><td>Rohini</td></tr>
<tr><td>Delhi</td></tr>
<tr><td>18</td></tr>
<tr><td>India</td></tr>
</table>
</html>

Currently the Output I am getting

<table border=1>
<td></td>
<td></td>
<td></td>
<td></td>
</table>

CodePudding user response:

Using sed

$ grep -hnr '$pattern' /path/of/file/ | sed -e 's~.*=\(.*\)~<tr><td>\1</td></tr>~;1i\<html><table border="1">' -e '$a\</table>\n</html>'
<html><table border="1">
<tr><td>ABC</td></tr>
<tr><td>Rohini</td></tr>
<tr><td>Delhi</td></tr>
<tr><td>18</td></tr>
<tr><td>India</td></tr>
</table>
</html>

You can also use the command directly from a file instead.

$ vi sed.script
s~.*=\(.*\)~<tr><td>\1</td></tr>~
1i\<html><table border="1">
$a\</table>\n</html>

To use, call the script with the -f flag

$ grep -hnr '$pattern' /path/of/file/ | sed -f sed.script
<html><table border="1">
<tr><td>ABC</td></tr>
<tr><td>Rohini</td></tr>
<tr><td>Delhi</td></tr>
<tr><td>18</td></tr>
<tr><td>India</td></tr>
</table>
</html>

CodePudding user response:

You can use this awk with delimiter ==:

grep -hnr "$pattern" /path/of/file/ |
awk -F '==' '
BEGIN { print "<html><table border='1'><tr>" }
{ print "<tr><td>" $2 "</td></tr>" }
END { print "</table>\n</html>" }'

<html><table border=1><tr>
<tr><td>ABC</td></tr>
<tr><td>Rohini</td></tr>
<tr><td>Delhi</td></tr>
<tr><td>18</td></tr>
<tr><td>India</td></tr>
</table>
</html>
  • Related