From dedbc1716026549e08044cded25128c914977d0f Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Sat, 24 Jun 2023 20:55:38 +0100 Subject: [PATCH] cut: Add `-s` option to suppress lines without field delimiters If this option is not specified, lines that contain to field delimiters will be printed unmodified. --- Base/usr/share/man/man1/cut.md | 1 + Userland/Utilities/cut.cpp | 10 +++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Base/usr/share/man/man1/cut.md b/Base/usr/share/man/man1/cut.md index e93e917336c..7fbed656c99 100644 --- a/Base/usr/share/man/man1/cut.md +++ b/Base/usr/share/man/man1/cut.md @@ -23,6 +23,7 @@ With no FILE, or when FILE is -, read standard input. * `-b` `--bytes=list`: Select only these bytes * `-f` `--fields=list`: select only these fields; also print any line that contains no delimiter character * `-d` `--delimiter=delim`: use `delim` instead of `tab` for field delimiter +* `-s`, `only-delimited`: suppress lines which don't contain any field delimiter characters ## Examples diff --git a/Userland/Utilities/cut.cpp b/Userland/Utilities/cut.cpp index 3931ed38ebe..c6824ea0318 100644 --- a/Userland/Utilities/cut.cpp +++ b/Userland/Utilities/cut.cpp @@ -134,11 +134,13 @@ static void process_line_bytes(StringView line, Vector const& ranges) outln(); } -static void process_line_fields(StringView line, Vector const& ranges, char delimiter) +static void process_line_fields(StringView line, Vector const& ranges, char delimiter, bool only_print_delimited_lines) { auto string_split = DeprecatedString(line).split(delimiter, SplitBehavior::KeepEmpty); if (string_split.size() == 1) { - outln("{}", line); + if (!only_print_delimited_lines) + outln("{}", line); + return; } @@ -157,6 +159,7 @@ ErrorOr serenity_main(Main::Arguments arguments) DeprecatedString byte_list = ""; DeprecatedString fields_list = ""; DeprecatedString delimiter = "\t"; + bool only_print_delimited_lines = false; Vector files; @@ -165,6 +168,7 @@ ErrorOr serenity_main(Main::Arguments arguments) args_parser.add_option(byte_list, "select only these bytes", "bytes", 'b', "list"); args_parser.add_option(fields_list, "select only these fields", "fields", 'f', "list"); args_parser.add_option(delimiter, "set a custom delimiter", "delimiter", 'd', "delimiter"); + args_parser.add_option(only_print_delimited_lines, "suppress lines which don't contain any field delimiter characters", "only-delimited", 's'); args_parser.parse(arguments); bool selected_bytes = (byte_list != ""); @@ -249,7 +253,7 @@ ErrorOr serenity_main(Main::Arguments arguments) if (selected_bytes) { process_line_bytes(line, disjoint_ranges); } else if (selected_fields) { - process_line_fields(line, disjoint_ranges, delimiter[0]); + process_line_fields(line, disjoint_ranges, delimiter[0], only_print_delimited_lines); } else { VERIFY_NOT_REACHED(); }