예제 #1
0
        private static Result Cut(Evaluator.Args args)
        {
            var separator   = args.ToString(args.GetSwitch("d") ?? " \t\r\n");
            var fieldsValue = args.GetSwitch("f");
            var fields      = fieldsValue != null
                ? args.Preview(args.ToScalar(fieldsValue))
                : "1";

            var fieldIndices = new HashSet <int>(fields
                                                 .Split(',')
                                                 .SelectMany(f => int.TryParse(f, out var idx) ? new[] { idx - 1 } : new int[0]));

            return(args
                   .Flatten()
                   .Select(obj => string
                           .Join(
                               separator,
                               args
                               .Preview(obj)
                               .Split(separator.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
                               .Where((_, idx) => fieldIndices.Contains(idx)))
                           .Trim())
                   .Where(s => s.Length > 0)
                   .ToArray());
        }