On Wed, Jun 04, 2025 at 09:43:00AM -0400, Chet Ramey wrote:
> On 6/2/25 5:58 AM, Duncan Roe wrote:
> > Hi,
> >
> > `cut -a ARRAY ...` puts its last line of output in ARRAY[0] and discards any
> > other elements ARRAY used to have. I tried 3 alternatives:
>
> Thanks for the report. Yes, sometimes marrying the multiple-line-oriented
> output of a tool like `cut' to one-dimensional arrays is awkward.
>
> This has to support output that consists of one field (bytes/chars) or
> multiple fields (well, fields). When you use -f to cut fields, each
> selected field is put into a separate element of the array. When you
> select bytes or characters, the selected bytes get put into array[0] as
> a single field. There's no current way to assign multiple array elements
> when you don't supply -f.

I totally missed that cut -a did that. This reinforces the main point of my
report which was that `help cut` should detail what the -a option does.
>
> The usual behavior of builtins that take array variable names as arguments
> is to unset the array before assigning anything, so I don't see any reason
> to change that. If you want to append to an existing array, there are ways
> to do that with array `cut' populates.
>
> I think the bigger issue is your alternative B: how to handle multiple
> lines of output with a single one-dimensional array.
>
> > Alternative B: empty out the array then put lines of output in successive
> > array elements.
>
> Is it useful to combine multiple selected fields (-f) into one space-
> separated field so `cut' can put the selected portions of each line into
> a corresponding array element?

I'm inclined to think not. Because, what if a field itself contains spaces?

The main thing is to document -a. I've attached a sample patch.

Cheers ... Duncan.
diff --git a/examples/loadables/cut.c b/examples/loadables/cut.c
index b3b8a477..6ac3cd8f 100644
--- a/examples/loadables/cut.c
+++ b/examples/loadables/cut.c
@@ -603,6 +603,10 @@ char *cut_doc[] = {
        "Columns correspond to bytes (-b), characters (-c), or fields (-f). 
The",
        "field delimiter is specified by -d (default TAB). Column numbering",
        "starts at 1.",
+       "",
+       "-a places the last line output by cut in the indexed array ARRAY.",
+       "-f fields are assigned to elements of ARRAY starting at index 0",
+       "otherwise ARRAY[0] holds the entire line.",
        (char *)NULL
 };
 

Reply via email to