Home > Software design >  Replace space with "NULL" in awk
Replace space with "NULL" in awk

Time:11-14

I am trying to convert a text file into HTML using awk command in shell script. Since the text file is auto-generated from server it contains server reponses, there are some empty values in the file as shown below

A 00
B 00
C 
D 
E 00

I want to replace this empty value with string "NULL" or "No response". Please suggest how it can be done. Table gererated from script for the above data

I have tried this

awk '{print "<tr>";for(i=1;i<=NF;i  ){
if($i==" ")
   {   
       print "<td>$i</td>";
   }
.........{some lines of code}
}'

Current Output enter image description here Expected Output enter image description here

CodePudding user response:

With your shown samples, please try following awk code. You need to use ternary operator to check if i variable is NULL then print string NULL else print its actual value itself.

awk '
{
  print "<tr>"
  for(i=1;i<=NF;i  ){ 
       print "<td>"($i!=""?$i:"NULL") "</td>";
   }
.........{some lines of code}
}'

CodePudding user response:

awk '{print "<tr>";for(i=1;i<=NF;i  ){
   {   
       print "<td>"$i"</td>";
   }
   NF < 2 {
       print "<td>NULL</td>";
   }
.........{some lines of code}
}'

Awk uses whitespace as field delimiters. Your data appears to have missing fields which you want replaced. This is one approach. If the number of fields is less than two, print a table cell with "NULL".

  • Related