tags:

views:

143

answers:

2

Hi folks,

I am writing an awk script that will take the output of grep and nicely format that into an HTML table. The delimiter is the ":" character; the problem I'm running into is that that character can appear in the text as well. So if I just use $1, $2, and $3 for the filename, line number, and comment respectively, I lose anything after the first : in the comment

Is there a way to say $1, $2, and then $3..NR without explicitly looping over the columns and concatenating them together?

Here's the script so far:

`

#!/usr/bin/awk

BEGIN {
    FS=":"

    print "<html><body>"
    print "<table>"
    print "<tr><td>File name</td><td>Line number</td><td>Comment</td></tr>"
}

{
    print "<tr><td>" $1 "</td><td>" $2 "</td><td>" $3 "</td></tr>"
}
END {
    print "</table>"
    print "</body></html>"

}`

And some sample input:

./mysql-connector-java-5.0.8/src/com/mysql/jdbc/BlobFromLocator.java:177:    // TODO: Make fetch size configurable
./mysql-connector-java-5.0.8/src/com/mysql/jdbc/CallableStatement.java:243:  // TODO Auto-generated method stub
./mysql-connector-java-5.0.8/src/com/mysql/jdbc/CallableStatement.java:836:  // TODO: Do this with less memory allocation
+1  A: 
{ print gensub(/^[^:]*:[^:]*:/,"","g") }
DigitalRoss
If you use awk with the command-line option `--re-interval`, you can use `/^([^:]*:){2}/`. More readable when you have more fields to delete.
andre-r
You mean, "if you use `gawk` ..."
glenn jackman
Yes, I mean `gawk`. (`awk` is often a symlink to `gawk`.)
andre-r
Heh, interval expressions. I *would* use them if I could frob the feature from within the script. It's silly for it to be a CLI switch.
DigitalRoss
Modified slightly:{name = $1; number = $2; comment = gensub(/^[^:]*:[^:]*:/, "", "g")}Ohh, good old Regexps.
I82Much
gensub didn't work on my unix box, though it did in cygwin. Weird.
I82Much
+1  A: 

BEGIN { FS=":"; OFS=":" } { name=$1; number=$2; $1=""; $2=""; comment=substr($0,3); }

ffx
This is clever. But you must be on a real Unix system or a BSD, for gawk I think you just need. `BEGIN { FS=":"; }` Even on oawk why set OFS?
DigitalRoss
I tried without modifying OFS at first, but the columns were changed to spaces in the comment field.
ffx