Home > Software engineering >  awk function is not printing output on the html page
awk function is not printing output on the html page

Time:04-30

shell scripting code:

$head=`echo "$value" | awk '{gsub(/% /,"\n");print;}'`;
$tail=`echo "$value" | awk '{gsub(/^ /,"\n");print;}'`;

html code:

$value1="<td><a href=\"$tail\">$head</a></td>";

sample input from user:

$value= %Link1^www.google.com%Link2^www.facdebook.com

output:

<a href="https://www.google.com/">Link1</a>

<a href="https://www.facebook.com/">Link2</a>

CodePudding user response:

With your shown samples, please try following awk program. Also while creating shell variable we need not to use $ make it without $ and while printing it only use it.

##Shell variable named value.
value="%Link1^www.google.com%Link2^www.facdebook.com"

Then following is the code need to be used for generating html code:

echo "$value" | 
awk -F'[\\^%]' '
{
  print "<a href=\"https://" $3"/\">" $2"</a>" ORS "<a href=\"https://" $5"/\">" $4"</a>"
}
'


Generic solution: In order to handle multiple inputs from variable, try following awk code.

echo "$value" | awk -F'[\\^%]' '
{
  for(i=2;i<=NF;i =2){
    print "<a href=\"https://" $(i 1)"/\">" $i "</a>"
  }
}
'
  • Related