Справочник по консольным командам Toybox для Android 12


  Ver.0.8.4     Ver.0.8.9     Pending  

Путь: Toys/POSIX, команды версии: Ver.4     Ver.9


comm

Комментарии в файле comm.c :

usage: comm [-123] FILE1 FILE2

Прочитайте ФАЙЛ1 и ФАЙЛ2, которые должны быть упорядочены, и выведите три текстовых столбцы в качестве вывода: строки только в FILE1; строки только в ФАЙЛЕ2; и линии в обоих файлах. Имя файла "-" является синонимом стандартного ввода.
  • -1 Подавить выходной столбец строк, уникальных для ФАЙЛА1
  • -2 Подавить выходной столбец строк, уникальных для ФАЙЛА2
  • -3 Подавить выходной столбец строк, дублирующихся в ФАЙЛ1 и ФАЙЛ2

  • usage: comm [-123] FILE1 FILE2

    Read FILE1 and FILE2, which should be ordered, and produce three text columns as output: lines only in FILE1; lines only in FILE2; and lines in both files. Filename "-" is a synonym for stdin.
  • -1 Suppress the output column of lines unique to FILE1
  • -2 Suppress the output column of lines unique to FILE2
  • -3 Suppress the output column of lines duplicated in FILE1 and FILE2

  • Исходный текст в файле comm.c

    #define FOR_comm
    #include "toys.h"
    
    static void writeline(const char *line, int col)
    {
      if (!col && FLAG(1)) return;
      else if (col == 1) {
        if (FLAG(2)) return;
        if (!FLAG(1)) putchar('\t');
      } else if (col == 2) {
        if (FLAG(3)) return;
        if (!FLAG(1)) putchar('\t');
        if (!FLAG(2)) putchar('\t');
      }
      puts(line);
    }
    
    void comm_main(void)
    {
      FILE *file[2];
      char *line[2];
      int i = 0;
    
      for (i = 0; i<2; i++) {
        file[i] = strcmp(toys.optargs[i], "-")?xfopen(toys.optargs[i], "r"):stdin;
        line[i] = xgetline(file[i]);
      }
    
      if (toys.optflags == 7) return;
    
      while (line[0] && line[1]) {
        int order = strcmp(line[0], line[1]);
    
        if (!order) {
          writeline(line[0], 2);
          for (i = 0; i < 2; i++) {
            free(line[i]);
            line[i] = xgetline(file[i]);
          }
        } else {
          i = order>0;
          writeline(line[i], i);
          free(line[i]);
          line[i] = xgetline(file[i]);
        }
      }
    
      // Print rest of the longer file.
      for (i = line[0] ? 0 : 1; line[i];) {
        writeline(line[i], i);
        free(line[i]);
        line[i] = xgetline(file[i]);
      }
    
      if (CFG_TOYBOX_FREE) fclose(file[0]), fclose(file[1]);
    }