0

I have the following file below with the format and I want to pass the value to HTML

BE64533-A0E1-4F98-A91F-02C1D0 column=ABC_Received:ABC_Structure_Type, timestamp=1439978656596, **value=ASCII**
BE64533-A0E1-4F8-A9F-03CE2C1D0 column=ABC_Received:Current_To, timestamp=1439978656596, **value=RPI** 

How do I get the value (ASCII and RPI) from this file and pass to STATIC HTML Tags. I have HTML tag below as where I want to enclose this value (ASCII and RPI)

<!--Row 1 Header -->
<tr>
<td class="HeaderCell">ABC_Structure_Type</td>
<td class="HeaderCell">Current_To</td>


<!--Row 1 Values -->
<tr>
<td  class="ValueCell">[ABC_Structure_Type]</td>
<td  class="ValueCell">[_Current_To]</td>

Output needed is as shown below

<!--Row 1 Values -->
<tr>
<td  class="ValueCell">ASCII</td>
<td  class="ValueCell">RPI</td>

From the file , They need to be replaced in the row 1 values [ABC_Structure_Type] and [Current_To] from the file that has value as ASCII and RPI respectively

2
  • What do you mean by "pass to static html tags"? Do you want to put html tags around those values? Please give an example, and the desired output.
    – chaos
    Aug 19, 2015 at 12:59
  • 1
    I'm sorry but it's still unclear; please show the exact output you want. Should ASCII and RPI replace ValueCell or should they replace [ABC_Structure_Type] and [_Current_To]?
    – chaos
    Aug 19, 2015 at 13:17

2 Answers 2

2

Well, it's not that clear, what you actually want to do, but it's clear, that you need to parse those values out of the file of yours. To do this, just put the values in separate arrays, like this (you need to set YourFile with the actual file with the values):

H=()
V=()
while read -r Line; do 
  H+=("$( printf "%s" "$Line" | grep -o 'column=[^,]*' | sed 's/^column=ABC_Received://')" )
  V+=("$( printf "%s" "$Line" | grep -o ' \*\*value=.*\*\*$' | sed 's/.*=\(.*\)\*\*$/\1/' )")
done < "$YourFile"

To check, that you got the arrays filled correctly, just run

printf "%s\n" "${H[@]}"
printf "%s\n" "${V[@]}"

If you just want to create a stub for a static HTML-page, you can do it now easily, if you edit the printfs above or with an easy loop (if you want to do something different with the values):

for v in "${H[@]}"; do 
  printf '<td class="HeaderCell">%s</td>\n' "$v"
done

for v in "${V[@]}"; do 
  printf '<td class="ValueCell">%s</td>\n' "$v"
done

Output with your example values would be:

<td class=HeaderCell>ABC_Structure_Type</td>
<td class=HeaderCell>Current_To</td>
<td class=ValueCell>ASCII</td>
<td class=ValueCell>RPI</td>

Of course, you can now do whatever you want with those values (not just printfs).

Disclaimer: GNU/bash script…

1

Well, assuming that a) the first two parts of the id-field are the key to group two lines together, and b) you may also use perl from the commandline, the following script will parse out the values - fields, and convert it to a html output from a given input-file:

#!/usr/bin/perl
my $fn=shift;
my ($ABC_found,$ASCII,$RPI,$ID)=(0,"","","");
open(FIN,"<",$fn) || die ("cannot open infile $fn");
sub print_html {
    $asc = shift;
    $rpi = shift;
print("
<html><body>
<!--Row 1 Header -->
<tr>
<td class='HeaderCell'>ABC_Structure_Type</td>
<td class='HeaderCell'>Current_To</td>
<!--Row 1 Values -->
<tr>
<td  class='ValueCell'>$asc</td>
<td  class='ValueCell'>$rpi</td>
</body></html>");
}

while (<FIN>) {
    if ($ABC_found==0 && m/^([\w\d]*)[\-]([\w\d]*).*ABC_Structure.*,        [\s]timestamp=.*,[\s]value=(.*)$/) {
        # print("ASCII : [$1]-[$2] : [$3]\n");
        $ABC_found=1;
        $ID="$1-$2";
        $ASCII=$3
    } elsif ($ABC_found==1 && m/^$ID.*Current_To.*[\s]timestamp=.*,[\s]value=(.*)$/) {
        $ABC_found=0;
        # print("RPI : [$1]\n");
        $RPI=$1;
        print_html($ASCII,$RPI);
    } else {    
        $ABC_found=0;
    }
}
close(FIN);

Usage:

$> perl script.pl inputfile.txt

Have fun !

Not the answer you're looking for? Browse other questions tagged .