On 24 July 2013 22:57, Janis Papanagnou <[email protected]> wrote:
>> Does ksh have an API to edit a file at a specific position pos1, read
>> n1 bytes, and write n1 bytes at that position without truncating the
>> file? I need this to do edit a file in place without reading and
>> writing it completely each time.
>
> Yes, ksh has seek redirection operators.
>
> Here's an ad hoc example (not sure whether the features are
> applied proficiently, but it seems to work :-)
>
> f=somefile
>
> cat <<EOT >"$f"
> Hello world!
> My good friend.
> How are you?
> EOT
>
> function fun ## palindrome (just for example)
> {
> arg=$1 x=
> (( len=${#arg} ))
> for ((i=0; i<len; i++))
> do res=${arg:i:1}$res
> done
> printf "%s" "$res"
> }
>
> pos1=18
> n1=8
>
> read -N "$n1" content 0<>"$f" <#((pos1))
> printf "%s" "$(fun "$content")" 1<>"$f" >#((pos1))
>
>
> Outputs:
>
> Hello world!
> My goneirf dod.
> How are you?
>
>
> Hope it's of some use to you.
How would this example look like if I opened a file descriptor with
the number 19? ksh seems to use file descriptors 0-9 for manual usage,
but descriptors opened with exec {filed}<>"name.rec" use numbers > 10.
Tina
--
Tina Harriott - Women in Mathematics
Contact: [email protected]
_______________________________________________
ast-users mailing list
[email protected]
http://lists.research.att.com/mailman/listinfo/ast-users