Inverse of the tee command?

Inverse of the tee command?

Post by Alan Bacsic » Tue, 31 Oct 1995 04:00:00




> Hi,

> Is there a command that performs the inverse of the "tee" command?

> "tee" takes data on stdin and writes it to stdout and another file.

> I'm looking for a function that takes data from stdin and another file
> and writes it to stdout.

> Does such a thing exist?

> If not, can someone tell me how I might go about coding this?

> Thanks,

> ==============================================================================
> Atul Mistry                            Smithsonian Astrophysical Observatory

> Phone: 617 496 7732                    Cambridge, MA  02138  USA
> Fax: 617 495 7356                      URL: http://hea-www.harvard.edu/~atm
> ------------------------------------------------------------------------------

Hope the following C source will help:

/************************************************************/
/* Program : Wye (Y). Combines 2 files line by line. */
/* Author : A.J. Bacsich. */
/* Date : 10/10/95 */
#include <stdio.h>
#define STRING 256
main(int argc, char **argv)
{  FILE *fp1, *fp2;
   char  line1[STRING], line2[STRING];

   if (argc != 3)
      { printf("Usage: wye <file1> <file2>\n");
      exit(-1);
   }
   if ( (fp1 = fopen(argv[1], "r")) == NULL)
      { printf("Cannot open file %s\n", argv[1]);
      exit(-1);
   }
   if ( (fp2 = fopen(argv[2], "r")) == NULL)
      { printf("Cannot open file %s\n", argv[2]);
      exit(-1);
   }
   /* both files open -- lets go! */
   while ( !feof(fp1) && !feof(fp2) )
      { if ((fgets(line1,STRING, fp1) != NULL)
         && (fgets(line2,STRING, fp2) != NULL))
            {
            fputs(line1,stdout); fputs(line2,stdout);
      }
   }
   if ( feof(fp1) && ! feof(fp2) )
      { /* file 2 longer print out rest */
      while (!feof(fp2))
         if (fgets(line2,STRING, fp2) != NULL)
            fputs(line2,stdout);
   }
   else
   if (feof(fp2) && ! feof(fp1))
      { /* file 1 longer print out rest */
      while (!feof(fp1))
         if (fgets(line1,STRING, fp1) != NULL)
            fputs(line1,stdout);
   }
exit(0);

Quote:}

/*************************************************************************/

This merges the 2 files line-by-line. It is easy enough to
edit so that one of the inputs is stdin.

                                Alan.

 
 
 

Inverse of the tee command?

Post by Arjun R » Wed, 01 Nov 1995 04:00:00




>>   /* both files open -- lets go! */
>>   while ( !feof(fp1) && !feof(fp2) )
>>      { if ((fgets(line1,STRING, fp1) != NULL)
>>         && (fgets(line2,STRING, fp2) != NULL))
>>            {
>>        fputs(line1,stdout); fputs(line2,stdout);
>>      }
>>   }

>As in: what if the *second* fgets returns NULL? Then, the fputs for
>the *first* fgets never gets executed. In other words, any time the
>second file has more lines than the first, the last line from the
>first file won't be merged.

Did I garble that clarification, or did I? :-(

It should have been: Any time the first file has more lines than
the second, the line from the first file that should follow the
last line in the second file won't be merged. E.g., if the first
file has 10 lines and the second has 5, the 6th line from the first
file will get suppressed.

* bug. Casual spot checks ("Is the first line OK? Is the last
line OK?") will miss it. But "wc -l" won't :-)

Cheers,
ar

 
 
 

1. Inverse of the tee command?

[Atul T. Mistry]

|   Is there a command that performs the inverse of the "tee" command?
|  
|   "tee" takes data on stdin and writes it to stdout and another file.
|  
|   I'm looking for a function that takes data from stdin and another file
|   and writes it to stdout.
|  
|   Does such a thing exist?

Yup.  The name is cat.  `cat - file' or `cat file -' depending on
whether you want stdin before or after the file.

- Harald

2. Output redirection

3. dd question: low level format?

4. Foolish OS battlers

5. proxy or address translation (nat) ?

6. vscript | tee -a out => tee is hanging

7. Duplicating STDOUT to a file (And yes, I know about the "tee" command)

8. Can you duplicate Standard Output WITHOUT tee command?

9. Performance problem with tee command

10. tee command taking the exported variable out ?!??