From: Karel Zak Date: Mon, 20 Jun 2022 11:25:59 +0000 (+0200) Subject: column: support ranges when addressing columns by numbers X-Git-Tag: v2.39-rc1~600 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=131a443459476301e73d900f6074ecb0102bf599;p=thirdparty%2Futil-linux.git column: support ranges when addressing columns by numbers Fixes: https://github.com/util-linux/util-linux/issues/1723 Signed-off-by: Karel Zak --- diff --git a/text-utils/column.1.adoc b/text-utils/column.1.adoc index 99559ac46b..a940a0536d 100644 --- a/text-utils/column.1.adoc +++ b/text-utils/column.1.adoc @@ -70,6 +70,7 @@ column names as defined by *--table-columns*, or names defined by *--table-column* or it's column number in order as specified by input. It's possible to mix names and numbers. The special placeholder '0' (e.g. -R0) may be used to specify all columns and '-1' (e.g. -R -1) to specify the last visible column. +It's possible to use ranges like '1-5' when addressing columns by numbers. *-J, --json*:: Use JSON output format to print the table, the option *--table-columns* is required and the option *--table-name* is recommended. diff --git a/text-utils/column.c b/text-utils/column.c index 50b24bd58d..cb40d8f26c 100644 --- a/text-utils/column.c +++ b/text-utils/column.c @@ -348,10 +348,24 @@ static void apply_columnflag_from_list(struct column_control *ctl, const char *l /* apply to columns specified by name */ STRV_FOREACH(one, all) { + int low = 0, up = 0; + if (strcmp(*one, "-") == 0) { unnamed = 1; continue; } + + /* parse range (N-M) */ + if (strchr(*one, '-') && parse_range(*one, &low, &up, 0) == 0) { + for (; low <= up; low++) { + cl = scols_table_get_column(ctl->tab, low); + if (cl) + column_set_flag(cl, flag); + } + continue; + } + + /* one item in the list */ cl = string_to_column(ctl, *one); if (cl) column_set_flag(cl, flag);