Quantcast
Channel: openSUSE Forums
Viewing all articles
Browse latest Browse all 40713

Split the file into multiple files at every line starts with STRING

$
0
0
I solved this by adding new parameter (--string=STRING) in split from coreutils.
New function string_split almost fullly inherited from function lines_split.

Code:

static void
--lines_split (uintmax_t n_lines, char *buf, size_t bufsize)
++string_split (char *str, char *buf, size_t bufsize)
{
  size_t n_read;
  char *bp, *bp_out, *eob;
  bool new_file_flag = true;
--  uintmax_t n = 0;
++  int n_str = strlen(str);

  do
    {
      n_read = full_read (STDIN_FILENO, buf, bufsize);
      if (n_read < bufsize && errno)
        error (EXIT_FAILURE, errno, "%s", infile);
      bp = bp_out = buf;
      eob = bp + n_read;
      *eob = '\n';
      while (true)
        {
          bp = memchr (bp, '\n', eob - bp + 1);
          if (bp == eob)
            {
              if (eob != bp_out) /* do not write 0 bytes! */
                {
                  size_t len = eob - bp_out;
                  cwrite (new_file_flag, bp_out, len);
                  new_file_flag = false;
                }
              break;
            }

          ++bp;
--          if (++n >= n_lines)
++          if (strncmp (bp, str, n_str) == 0)
            {
              cwrite (new_file_flag, bp_out, bp - bp_out);
              bp_out = bp;
              new_file_flag = true;
--              n = 0;
            }
        }
    }
  while (n_read == bufsize);
}


Viewing all articles
Browse latest Browse all 40713

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>