Esempio n. 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());
        }
Esempio n. 2
0
        private static Result Uniq(Evaluator.Args args)
        {
            var groups = args.Flatten().GroupBy(obj => args.Preview(obj));

            if (args.HasSwitch("c")) // count objects in each group
            {
                return(groups
                       .Select(grp => $"{grp.Count()}\t{args.Preview(grp.First())}")
                       .ToArray());
            }
            else
            {
                return(groups
                       .Select(grp => grp.First())
                       .ToArray());
            }
        }
Esempio n. 3
0
        private static Result Grep(Evaluator.Args args)
        {
            var pattern = args[0];
            var flip    = args.HasSwitch("v");

            return(args
                   .Skip(1)
                   .SelectMany(result => result)
                   .Where(obj => flip ^ args.Matches(args.Preview(obj), pattern))
                   .ToArray());
        }
Esempio n. 4
0
        private static Result Uniq(Evaluator.Args args)
        {
            var fieldToGroupBy = args.GetStrSwitch("k", null);
            Func <object, object> keySelector = o => o;

            if (!string.IsNullOrEmpty(fieldToGroupBy))
            {
                keySelector = o => args.Eval.Resolve(o).Properties.FirstOrDefault(p => p.Name == fieldToGroupBy)?.Value;
            }

            var aa     = args.Flatten();
            var groups = aa.GroupBy(obj => args.Preview(keySelector(obj)));

            if (args.HasSwitch("c")) // count objects in each group
            {
                return(groups
                       .Select(grp =>
                {
                    return new ObjectInfo(
                        preview: $"{grp.Count()}: {grp.Key}",
                        properties: new[]
                    {
                        new Property(name: "Key", value: grp.Key),
                        new Property(name: "Count", value: grp.Count()),
                        new Property(name: "Elems", value: grp.ToArray())
                    });
                })
                       .ToArray());
            }
            else
            {
                return(groups
                       .Select(grp => grp.First())
                       .ToArray());
            }
        }
Esempio n. 5
0
        private static Result Sort(Evaluator.Args args)
        {
            var objs = args.Flatten();

            var fieldToSortBy = args.GetStrSwitch("k", null);
            Func <object, object> keySelector = o => o;

            if (!string.IsNullOrEmpty(fieldToSortBy))
            {
                keySelector = o => args.Eval.Resolve(o).Properties.FirstOrDefault(p => p.Name == fieldToSortBy)?.Value;
            }

            IComparer <object> comparer = args.HasSwitch("n")
                ? Comparer <object> .Create((lhs, rhs) => Comparer <long?> .Default.Compare(args.Eval.TryToNumber(lhs), args.Eval.TryToNumber(rhs)))
                : Comparer <object> .Create((lhs, rhs) => Comparer <string> .Default.Compare(args.Preview(lhs), args.Preview(rhs)));

            var ordered = objs.OrderBy(keySelector, comparer);

            var finalOrder = args.HasSwitch("r") // reverse
                ? ordered.Reverse()
                : ordered;

            return(finalOrder.ToArray());
        }