0

I'm newby in AWK and I know its not a specific question. I just need some advice how should i do this.

Given the following names in a file, author.list:


KOVACS PETER
Kiss Roland
Nagy jolan
Lisztes Tibor
Feher aNDRas
Korma Maria
Akarki Jack

write an AWK program that can read the names from the file and print them to an html table with a three-column format in an output file, output.html. The table should render like this:

    Kovacs Peter     Lisztes Tibor    Akarki Jack
    Kiss Roland      Feher Andras
    Nagy Jolan       Korma Maria

An example execution:

awk -f convert.awk author.list > output.html

Ensure that output.html is a valid html file.

4
  • 1
    What have you tried so far? And who thought this was a good exercise for learning AWK? Commented Apr 3, 2020 at 12:01
  • @Cerberus, edit you question. And do it according to the feedback you receive here in the comments. Commented Apr 3, 2020 at 12:09
  • You say The table should render like this but we don't care about that, we care about helping you generate the HTML that would cause the table to render like this. So, edit your question to show the HTML you want the awk script to output as THAT is what you're (hopefully!) really asking us for help with. Commented Apr 3, 2020 at 12:20
  • 2
    Please don't make more work for others by vandalizing your posts. By posting on the Stack Exchange (SE) network, you've granted a non-revocable right, under a CC BY-SA license, for SE to distribute the content (i.e. regardless of your future choices). By SE policy, the non-vandalized version is distributed. Thus, any vandalism will be reverted. Please see: How does deleting work? …. If permitted to delete, there's a "delete" button below the post, on the left, but it's only in browsers, not the mobile app. Commented May 11, 2020 at 0:29

1 Answer 1

1

Without seeing the HTML you want to generate it's a guess but this might be what you want:

$ cat tst.awk
BEGIN {
    print "<html>"
    print "  <table>"
}
{
    for (i=1; i<=NF; i++) {
        $i = toupper(substr($i,1,1)) tolower(substr($i,2))
    }
    if ( (NR%3) == 1 ) {
        if (NR>1) print "      </tr>"
        print "      <tr>"
    }
    printf "        <td>%s</td>\n", $0
}
END {
    for (i=NR+1; (i%3) != 1; i++) {
        printf "        <td>%s</td>\n", ""
    }
    print "      </tr>"

    print "  </table>"
    print "</html>"
}

.

$ awk -f tst.awk author.list
<html>
  <table>
      <tr>
        <td>Kovacs Peter</td>
        <td>Kiss Roland</td>
        <td>Nagy Jolan</td>
      </tr>
      <tr>
        <td>Lisztes Tibor</td>
        <td>Feher Andras</td>
        <td>Korma Maria</td>
      </tr>
      <tr>
        <td>Akarki Jack</td>
        <td></td>
        <td></td>
      </tr>
  </table>
</html>

The name upper/lower case conversion will fail on names like McDonald or O'Hara or Billy-Bob that don't only have 1 capital letter at the start of the name. If you have to handle that then you need to provide an algorithm.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.