Append filename to beginning of output

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Append filename to beginning of output

gpatrick
I am tailing a file and any changes are written to a new file, for example:

new line1
new line2
new line3

But, what I need is to:

tfile:new line1
tfile:new line2
tfile:new line3

Where tfile is the file that is being tailed and the output is written to a new file.

This is what I have that works, except for inserting the 'tfile:' at the beginning.

import java.io.

class Tail
method Tail(f=File) public
pos = int 0
filex = RandomAccessFile(f,"r")
pos = filex.length() - Math.min(400,filex.length())
filex.seek(pos)
rm="/bin/rm"
outPath='/tmp/k.out'
Runtime.getRuntime().exec(rm outPath)
outFile=FileWriter(outPath)
dest=BufferedWriter(outFile)
loop forever
  l = filex.length()-pos
  buf=byte[256]
  read = filex.read(buf,0,256)
  out = String(buf,0,256)
  --say out
  dest.write(out,0,out.length())
  dest.newline()
  dest.flush()
  pos=pos+1
end

method main(args=String[]) public static
Tail(File(args[0]))

If I try "dest.write(f':'out,0,out.length())" it still just gives me "out" but not the filename.

Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: Append filename to beginning of output

Kermit Kiser
To add the file name, you need to request it first and then add it to
the write with adjustment for the name length:

   fn=f.getName()
   fnl=fn.length()
   dest.write(fn':'out,0,out.length()+fnl+1)

-- Kermit

On 10/14/2013 8:29 AM, gpatrick wrote:

> I am tailing a file and any changes are written to a new file, for example:
>
> new line1
> new line2
> new line3
>
> But, what I need is to:
>
> tfile:new line1
> tfile:new line2
> tfile:new line3
>
> Where tfile is the file that is being tailed and the output is written to a
> new file.
>
> This is what I have that works, except for inserting the 'tfile:' at the
> beginning.
>
> import java.io.
>
> class Tail
> method Tail(f=File) public
> pos = int 0
> filex = RandomAccessFile(f,"r")
> pos = filex.length() - Math.min(400,filex.length())
> filex.seek(pos)
> rm="/bin/rm"
> outPath='/tmp/k.out'
> Runtime.getRuntime().exec(rm outPath)
> outFile=FileWriter(outPath)
> dest=BufferedWriter(outFile)
> loop forever
>    l = filex.length()-pos
>    buf=byte[256]
>    read = filex.read(buf,0,256)
>    out = String(buf,0,256)
>    --say out
>    dest.write(out,0,out.length())
>    dest.newline()
>    dest.flush()
>    pos=pos+1
> end
>
> method main(args=String[]) public static
> Tail(File(args[0]))
>
> If I try "dest.write(f':'out,0,out.length())" it still just gives me "out"
> but not the filename.
>
> Thanks.
>
>
>
> --
> View this message in context: http://ibm-netrexx.215625.n3.nabble.com/Append-filename-to-beginning-of-output-tp4026810.html
> Sent from the ibm-netrexx mailing list archive at Nabble.com.
>
> _______________________________________________
> Ibm-netrexx mailing list
> [hidden email]
> Online Archive : http://ibm-netrexx.215625.n3.nabble.com/
>
>
>

_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

Reply | Threaded
Open this post in threaded view
|

Re: Append filename to beginning of output

gpatrick
Thank you