2

I'm working with a software which reads a trace file with multiple entries, each of which are of format:

<...>,<...>,<64 character string>

This 64 character string is in my case constitute entirely of '0'.

Would it be possible to have something like sed for file streams (not stdin, file stream opened using open syscall) which would allow me to feed the said program a file with following entries:

<...>,<...>,&

and the program would replace '&' with 64 '0's. The problem with using sed on the trace file itself before executing the program is the limited space available on the machine.

My current plan is to use ptrace to intercept read syscalls and make it appear for the program as if it is reading a normal file, would that be a good idea?


(edit)
The program parses the file only once.

7
  • 1
    @Mawg you are right, it belongs to unix.stackexchange more than it does here. Is it possible to move this question there? Thanks
    – lol
    Dec 20, 2018 at 14:32
  • A moderator can, but I can't :-/ I have flagged it, so hopefully it will be migrated. Wait a day or so, and if no migration, then delete this & post again there, I guess. If they can't help on Unix & Linux, we can probably recommend an app for it on S/W recommendations.
    – Mawg
    Dec 20, 2018 at 14:36
  • Is the file opened by the program a file named on the command line? In that case you should still be able to give it /dev/stdin as the filename while piping sed to it.
    – Kusalananda
    Dec 20, 2018 at 17:22
  • @Kusalananda That worked, thanks! Consider answering the question.
    – lol
    Dec 20, 2018 at 17:55
  • @Kusalananda Would this work if the program didn't read the input file line by line?
    – lol
    Dec 20, 2018 at 18:02

1 Answer 1

1
sed 's/,[^,]*$/,\&/' <file | program /dev/stdin

This assumes that the program must be given the filename on the command line and that it does not try to do seeks backwards or forwards in the file (standard input is not generally seekable).

The special file /dev/stdin will contain the contents of the standard input stream of the process. Above, we use this fact to send the modified contents of file to the program using a standard Unix pipe on the command line, even though the program normally does not read its standard input stream.

The sed expression will replace everything after the last comma on each line of file with a &.

2
  • What if the program seeks through the file? Can the output be written to some other file?
    – lol
    Dec 20, 2018 at 18:21
  • 1
    @lol I'm not 100% certain, but skipping forwards may possibly be ok, but I would avoid it (I simply haven't tested this). The ideal situation would be to fully preprocess the input file and then give it to the program.
    – Kusalananda
    Dec 20, 2018 at 18:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .