Sorting a list by a random order and then trimming it: How to use Variables in Unix commands?

I am trying to make a Macro that can do the following:

  1. Ask the user for:
    a) an input number (stored in local__Num)
    b) if the user wants to subtract that number from 60 or 90 (stored in local__Option)
  2. Subtract local__Num from local__Option to produce local__Action).
  3. Sort a list randomly
  4. Trim the list by the value of local__Action

To sort the list, I am using the Unix command “sort -R”. It works perfectly.

To trim the list by the value of local__Action, I used the Unix command sed '$KMVAR_local__total,$ d’.

This seems to stop the macro from working as it produces an error message:
Screenshot 2022-06-02 at 18.56.58

If I change the SED command to a hardcoded value such as sed '10,$ d' then it works perfectly.
So I think the issue is with using a variable in the command.

Any ideas?

Here is my Macro with the variable. (7.5 KB)

Here is my Macro with the hardcoded Sed (7.5 KB)

Your sed command uses single quotes. Single quotes prevent variable expansion by the shell, so you are using the literal string $KMVAR_local___total and it doesn't work.

You could just use double-quotes, but I'm worried about that $ sign near the end. So play safe -- try

sed '10,'"${KMVAR_local___total}"',$ d'

Thanks for the help! I tried sed '10,'"${KMVAR_local___total}"',$ d' but it still comes up with this error message:
Screenshot 2022-06-03 at 18.09.16

Sorry, I confused your two seds and left the 10 from your "static code" example in the reply.

Principle remains the same -- single quotes stop your variable from being expanded. Try:

sed "${KMVAR_local__total}"',$ d'

(That's "close-bracket, double-quote, single-quote, comma" in the middle, in case it isn't clear.)

1 Like

I’m late, but have you considered using this?

head -n $KMVAR_local__total

I think it does the same thing—it might have an off-by-one error that’s easily fixed—and avoids the tricky quoting of the sed command.

1 Like

@Nige_S @drdrang Thanks! Both work. The head command is slightly better as the sed command I am using is off by one.
Thanks!