How to do i.e. 'sort <file >file'?

How to do i.e. 'sort <file >file'?

Post by Martin Kreutz » Fri, 13 Aug 1993 04:07:13



I would like to write commands which use the same file
for input and output like:
        sort <file >file

On the OS9 operating system I had a command "into" with
that I could write:
        sort <file | into file

Now I'm looking for a similar program for UNIX.

Thanks in advance

Martin

--
__________
Martin Kreutzer        | Fraunhofer Gesellschaft IIS | Phone: +49-9131-776-325

                       | D-91058 Erlangen            |

 
 
 

How to do i.e. 'sort <file >file'?

Post by Jim Dav » Sat, 14 Aug 1993 09:22:41


:I would like to write commands which use the same file
:for input and output like:
:        sort <file >file

sort has an option for that: 'sort file -o file'.
:
:On the OS9 operating system I had a command "into" with
:that I could write:
:        sort <file | into file
:
:Now I'm looking for a similar program for UNIX.

Look at the 'overwrite' shell script on page 154 of Kernighan and
Pike's _The UNIX Programming Environment_.
--
Jim Davis               | "Kid, squid.  It's that simple."


 
 
 

How to do i.e. 'sort <file >file'?

Post by Jeffrey M. Osi » Sat, 14 Aug 1993 10:30:04


Might have to use a temporary file, as in:

  sort < file > /tmp/ralph ; mv /tmp/ralph file

That'd be the way I'd do it, anyway.

Jefro
__________________________________________________________________________
Jeffrey Osier                                           Cygnus Support

+1 415 903 1409                                         Mountain View, CA
+1 415 903 0122 (fax)                                   94043, USA
                Ejfl;kha;hggyuuopij  <- kerberized Jefro

 
 
 

How to do i.e. 'sort <file >file'?

Post by Tom Christianse » Sat, 14 Aug 1993 10:23:57



:I would like to write commands which use the same file
:for input and output like:
:        sort <file >file
:
:On the OS9 operating system I had a command "into" with
:that I could write:
:        sort <file | into file
:
:Now I'm looking for a similar program for UNIX.

Pretty easy to write.  Here it is:

    #!/bin/sh

    cat > $1.into.$$ && mv $1.into.$$ $1

Except I fear that you will almost certainly find that your vendor doesn't
have have cat check all its write()s as well as to fclose(stdout)
[depending on your implementation] and exit appropriately if the
filesystem fills up, so what you have there may be a problem.  Sigh.  
This is really something that the shell should be good for, if the damn
utilities weren't all so broken.  I just tried on SunOS 4.1.3, and
it was brain-dead.

Sigh, I really wanted to give a purely shell solution this time guys,
I really did!

Oh well....

    #!/usr/bin/perl
    $BUFSIZ = 4 * 2**10;
    if ($ARGV[0] eq '-a') { shift; $mode = '>>'; }
    else                  { $mode = '>'; }

    $into = shift;
    open (INTO, "+$mode $into") || die "can't write $into: $!";
    $buffer .= $chunk while ($count = sysread(STDIN, $chunk, $BUFSIZ)) > 0;
    die "error reading stdin: $!" if !defined $count;
    syswrite(INTO, $buffer, length($buffer)) == length($buffer)
        || die "error writing $into: $!";
    close(INTO) || die "can't close $into: $!";

That doesn't use a temp file, but it does use memory.  This one uses
a temp file but not much memory.

        #!/usr/bin/perl

        $into = shift;
        $tmp = "$into.into.$$";
        open (TMP, "> $tmp")                       || die "can't open $tmp for write: $!";
        while (<STDIN>) { print(TMP  $_)  || die "can't write $tmp: $!"; }
        close (TMP)                             || die "can't close $tmp: $!";
        rename($tmp, $into)                     || die "can't rename($tmp,$into): $!";

At least it checks for error conditions.  Damned shell utils -- cain't trust 'em.

If you want to know about timing, here's the shell method:

    0.030u 0.250s 0:00.70 40.0% 0+139k 3+21io 3pf+0w
    0.030u 0.910s 0:03.18 29.5% 0+166k 8+153io 2pf+0w

Here's the one without a temp file:

    0.070u 0.270s 0:00.40 85.0% 0+495k 0+9io 2pf+0w
    1.080u 1.640s 0:07.43 36.6% 0+2559k 39+72io 40pf+0w

    (you can reduce the system time using a bigger buffer; here's 32k

    1.090u 1.290s 0:03.19 74.6% 0+2536k 0+73io 0pf+0w

And here's the one with:

    0.330u 0.210s 0:00.71 76.0% 0+549k 0+12io 0pf+0w
    1.930u 1.030s 0:04.14 71.4% 0+710k 35+88io 26pf+0w

The first number is against /etc/termcap, and the second against /vmunix.

    -rw-rw-r--  1 root       103113 Aug  3 20:49 /etc/termcap
    -rwxr-xr-x  2 root      1733443 Apr 27 00:24 /vmunix

Hm... if I use

--tom
--

 
 
 

How to do i.e. 'sort <file >file'?

Post by Tom Christianse » Sat, 14 Aug 1993 10:43:20



:Look at the 'overwrite' shell script on page 154 of Kernighan and
:Pike's _The UNIX Programming Environment_.

Good reference.   Alas, they aren't checking for errors at all either.

--tom
--

 
 
 

How to do i.e. 'sort <file >file'?

Post by Harald.Eik.. » Mon, 16 Aug 1993 06:44:01


I have this tiny shell script called "apply".  However, it may only be used
together with commands (programs) that will take a single file argument last
in their command arg list.  That is to say; use with care.

==== apply ====
#!/bin/sh

case $# in [01])
    echo >&2 "usage: $0 command file"
    exit 64
esac

parg= clear=1
for arg do
    case $clear in
      1) set x; clear=;;

    esac
    parg=$arg
done
shift
file=$arg

cat <<!

!

============

  ~~h

 
 
 

How to do i.e. 'sort <file >file'?

Post by BRANDAUER CARL » Mon, 16 Aug 1993 06:26:17




>:I would like to write commands which use the same file
>:for input and output like:
>:        sort <file >file

Use System V and:

        sort -o file file

Is there anything wrong with this solution?

Cheers - Carl

 
 
 

How to do i.e. 'sort <file >file'?

Post by Tom Christianse » Mon, 16 Aug 1993 09:32:25



:

:>:I would like to write commands which use the same file
:>:for input and output like:
:>:        sort <file >file
:
:Use System V and:
:
:       sort -o file file
:

Three things:

1) You expect me to use System V.  
2) It only works with sort and is not a generic solution.
3) It doesn't work everywhere.

--tom
--

 
 
 

How to do i.e. 'sort <file >file'?

Post by Roy Johns » Thu, 19 Aug 1993 02:52:49




   :

   :>:I would like to write commands which use the same file
   :>:for input and output like:
   :>:        sort <file >file
   :
   :Use System V and:
   :
   :    sort -o file file
   :

   Three things:

   1) You expect me to use System V.  
   2) It only works with sort and is not a generic solution.
   3) It doesn't work everywhere.

1. On Sun, at least, this is the behavior of SysV and non-SysV.
2. For the generic case, we have a program called "into" that I
   don't know where it came from.  Here's a tidbit from the man
   page:

NAME
     into - copy into a file without destroying it

SYNOPSIS
     into [ -f ] outfile

DESCRIPTION
     Into copies its standard input into the  specified  outfile,
     but  doesn't  actually  modify  the  file until it gets EOF.
Sun Release 4.1    Last change: Utah 12/17/84                   1

3. (and Tom should approve) you can write "into" (or an "improved"
   variation on it if you like, which would take a command and a
   filename and run the command on the filename, outputting back
   into the filename) in Perl pretty easily.

--

"When the only tool you have is Perl, the whole | "Hooray for snakes!"
 world begins to look like your oyster." -- Me  |  -- The Simpsons (29 Apr 93)

 
 
 

1. How to do i.e. "sort <file >file"?

I would like to write commands which use the same file
for input and output like:
        sort <file >file

On the OS9 operating system I had a command "into" with
that I could write:
        sort <file | into file

Now I'm looking for a similar program for UNIX.

Thanks in advance

Martin
--
__________
Martin Kreutzer                 Fraunhofer Gesellschaft - IIS
                                Wetterkreuz 13

2. /dev/dsp and /dev/video over network

3. Q: file's size <> file's block count

4. pf rules on dial-up

5. <><><> MOUNTING EXTENDED PARTITION <><><>

6. Q: Something similar to Streams in Linux?

7. Wanted: <><><> Unix Specialist <><><>

8. UNSUBSCRIBE

9. LILO help <><><><><><>

10. Does the new <File> </File> directive work?

11. ----->>>XF86Config Help Needed Here's a Copy of<<<------

12. <Files> </Files> tags

13. Expected </Files> but saw </Files>??