[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Read a fixed length of input each time
From: |
Peng Yu |
Subject: |
Re: Read a fixed length of input each time |
Date: |
Tue, 23 Jun 2020 11:38:32 -0500 |
It is to deal with binary files. For example, I may create a binary
stream using the following Go file.
```
// main.go
package main
import (
"bufio"
"strconv"
"encoding/binary"
"io"
"os"
"log"
)
func main() {
stdin := bufio.NewReader(os.Stdin)
for {
line, err := stdin.ReadBytes('\n')
if err == io.EOF {
if len(line) == 0 { break }
} else {
if err != nil { log.Fatal(err) }
line = line[:(len(line)-1)]
}
x, err := strconv.ParseUint(string(line), 0, 32)
if err != nil { log.Fatal(err) }
err = binary.Write(os.Stdout, binary.LittleEndian, uint32(x))
if err != nil { log.Fatal(err) }
}
}
```
Then I may need to deal with the binary output of main.go.
$ seq 10 | go run main.go | xxd
00000000: 0100 0000 0200 0000 0300 0000 0400 0000 ................
00000010: 0500 0000 0600 0000 0700 0000 0800 0000 ................
00000020: 0900 0000 0a00 0000 ........
On 6/23/20, Andrew J. Schorr <aschorr@telemetry-investments.com> wrote:
> On Tue, Jun 23, 2020 at 09:53:57AM -0500, Neil R. Ormos wrote:
>> RS="................"
> ...
>> Then, each getline will place chunk-size characters in RT, provided there
>> are enough characters available to match RS. Otherwise, the residual
>> characters on the final getline resulting in reaching the end-of-file will
>> be placed in the variable specified to receive the results from getline.
>
> That's a creative solution to the problem.
>
>> [*1] I acknowledge the warnings from the developers and their suggestions
>> that reading binary data can best be done with an extension or some
>> pre-processing step. But those solutions may not be available or uniform
>> in all environments where gawk is available. So, even if this RS-based
>> method is not as good, it might allow the user to write a relatively
>> portable program intended for several heterogeneous environments.
>
> I'll bite -- what's the benefit of reading binary data like this? What do
> you
> do with it once you get it inside gawk? It's easy enough to add read and
> write
> functions in an extension library, but I've never understood the usage
> case.
> It would take maybe 15 minutes to add these functions to the select
> extension,
> but I need a reason. And yes, as you point out, an extension is an
> additional
> installation dependency.
>
> Regards,
> Andy
>
>
--
Regards,
Peng
- Read a fixed length of input each time, Peng Yu, 2020/06/22
- Re: Read a fixed length of input each time, arnold, 2020/06/23
- Re: Read a fixed length of input each time, Neil R. Ormos, 2020/06/23
- Re: Read a fixed length of input each time, Andrew J. Schorr, 2020/06/23
- Re: Read a fixed length of input each time,
Peng Yu <=
- Re: Read a fixed length of input each time, Andrew J. Schorr, 2020/06/23
- Re: Read a fixed length of input each time, Peng Yu, 2020/06/23
- Re: Read a fixed length of input each time, Andrew J. Schorr, 2020/06/23
- Re: Read a fixed length of input each time, Peng Yu, 2020/06/23
- RE: Read a fixed length of input each time, Tom Gray, 2020/06/23
- Re: Read a fixed length of input each time, Neil R. Ormos, 2020/06/23
- Re: Read a fixed length of input each time, Andrew J. Schorr, 2020/06/23
- Re: Read a fixed length of input each time, Neil R. Ormos, 2020/06/23