On 5/2/2008 12:52 PM, Rahul wrote:
> Is there something akin to a "heredoc" in awk? I need to process a set
of
> records, extract relevant numbers from each record and then create a
> formatted multi-line output for each record. Problem is that the output
> would contain a lot of special characters and using a normal print or
> printf would need me to do some nasty quoting (or not?)
>
> The desired output snippet for each record is something like this:
>
> {"id":"town",
> "label":"middleton",
> "x_size":"10.22",
> "y_size":"5.22",
> },
>
>
> Of course, I'd be using $1 (or equivalent variables) for the town,
> middleton, 10.22, 5.22 etc.
>
> Any constructs like the familiar bash
>
> print << EOF
> static blah blah
> "${var_to_be_inserted}"
> more static blah blah
> EOF
>
Is this what you're looking for:
$ cat file
town middleton 10.22 5.22
$ awk -v fmt='{"id":"%s",
"label":"%s",
"x_size":"%s",
"y_size":"%s",
},
' '{printf fmt,$1,$2,$3,$4}' file
{"id":"town",
"label":"middleton",
"x_size":"10.22",
"y_size":"5.22",
},
If not, please clarify with some small set of sample input and the
expected
output for that input.
Regards,
Ed.


|