-1

I have conf file like conf.txt with the next value

var=somevalue1
var1=somevalue2
var2=somevalue3
var3=http://somevalue1

The idea is to prepare this into HTML page with table like

 <tr>
    <td>var</td>
    <td>somevalue1</td>
  </tr>
 <tr>
    <td>var3</td>
    <td><a href="http://somevalue1">http://somevalue1</a></td>
  </tr>

The question is - what does best way to use to prepare it?

So I can read from a file with readarray And then use something like awk to split it. Unfortunately, I am good in powershell, but really bad with bash.

Could you please give some advice or example?

I am planning to put this into var and then put this var to EOF to create a page

  • This is plain old keyword substitution. The syntax of your consiguration file is slightly wacky but `sed -r 's%([^=]+)=(.*)%s/\1/\2/g%' conf.txt` will turn it into a simple `sed` script. – tripleee Sep 08 '21 at 18:32
  • generally speaking ... no need for `bash/readarray` as `awk` can read, parse and (re)format the output; you just need to decide on how many different types of input you can have and the associated output templates (based on your examples you have generic variables and URLs; is that all?); what have you tried so far? if you're good with `powershell` then why not stick with `powershell` (ie, why `bash`)? – markp-fuso Sep 08 '21 at 18:32
  • I wouldn't do this in shell at all. There are numerous libraries in more capable languages for generating documents from arbitrary templates. – chepner Sep 08 '21 at 18:57
  • I use Jenkins pipeline where I do not plan to install libraries and other languages and where I have only bash. When the job is finished I have some output variables that I want to prepare in HTML table. In this file I have URLs and general value – Kirill Kiselev Sep 08 '21 at 19:48
  • "Only Bash" is not realistic, you will need at least a base set of utilities like `cat` and `grep` in order to be able to do much anything; usually this base set includes `sed` and Awk, too. – tripleee Sep 09 '21 at 05:43

1 Answers1

1

Assumptions:

  • only need to worry about 2x data types ... 1) basic variable and 2) URL
  • all URL data starts with the string http
  • the data may contain embedded equal signs

Sample data:

$ cat conf.dat
var=somevalue1
var1=somevalue2
var2=somevalue3
var2x=somevalue3=345                        # value includes embedded "="
var3=http://somevalue1
var4=http://somevalue1/index.html&id=35     # value includes embedded "="

One awk idea:

awk -F'=' '
             { newvalue=""                         # in case value includes embedded equal signs ...
               sep=""
               for (i=2;i<=NF;i++) {               # loop through fields 2 thru NF ...
                   newvalue = newvalue sep $(i)    # appending to new "=" delimited string
                   sep=FS
               }
             }

$2 ~ /^http/ { print "<tr>\n\t<td>" $1 "</td>\n\t<td><a href=\"" newvalue "\">" newvalue "</a></td>\n</tr>\n"
               next
             }

             { print "<tr>\n\t<td>" $1 "</td>\n\t<td>" newvalue "</td>\n</tr>\n" }

' conf.dat

This generates:

<tr>
        <td>var</td>
        <td>somevalue1</td>
</tr>

<tr>
        <td>var1</td>
        <td>somevalue2</td>
</tr>

<tr>
        <td>var2</td>
        <td>somevalue3</td>
</tr>

<tr>
        <td>var2x</td>
        <td>somevalue3=345</td>
</tr>

<tr>
        <td>var3</td>
        <td><a href="http://somevalue1">http://somevalue1</a></td>
</tr>

<tr>
        <td>var4</td>
        <td><a href="http://somevalue1/index.html&id=35">http://somevalue1/index.html&id=35</a></td>
</tr>
markp-fuso
  • 28,790
  • 4
  • 16
  • 36