3

According to this cut command lacks --complement option. Any suggestions as how to get this?

I needed this as it is supported on Linux sh

My problem is quite similar to this so I tried following to make it work

#!/bin/sh
EXTRA=$@
REST=`echo $EXTRA | cut -d ' ' --complement -s -f1`
echo $REST

Any suggestions are welcome

PS: I cannot use bash instead of sh

Carmen Sandoval
  • 2,266
  • 5
  • 30
  • 46
Anurag Dixit
  • 387
  • 3
  • 8
  • 2
    Since you know `cut` on OSX doesn't support `--complement`, why would you think this would work? Can you explain what you are actually trying to accomplish, as there may be a way to do it that doesn't involve using this feature of `cut` (in fact, may not use `cut` at all). – Scott Hunter Aug 24 '17 at 01:16
  • 2
    to amplify on ScottHunter 's comment, your Q needs small sample set of input, and required output from that input, your current output and exact text of any error messages. Good luck. – shellter Aug 24 '17 at 02:03
  • `EXTRA=$@` is not a useful thing to do, which makes me doubt that `cut` is the solution to what you want in the first place. – chepner Aug 24 '17 at 13:46

4 Answers4

6

The correct way to solve this for generic use-cases with --complement, would be:

brew install coreutils

as gnu-cut is part of the pkg.

Hashfyre
  • 240
  • 3
  • 6
1

You can install gcut, the GNU version of cut using homebrew.

brew install coreutils

That one supports --complement (as well as --output-delimiter).

Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
Carmen Sandoval
  • 2,266
  • 5
  • 30
  • 46
0

If you really just want all but the first argument, use shift.

#!/bin/sh
first_arg=$1
shift
printf '%s\n' "$@"  # Prints all but the first argument, one per line
chepner
  • 497,756
  • 71
  • 530
  • 681
-1

Using minus at the end without anything else is equivalent to --complement

#!/bin/sh
echo test asdf test | cut -d ' ' -s -f2-

For example the above example is equivalent to

#!/bin/sh
echo test asdf test | cut -d ' ' -s -f1 --complement
love2code
  • 441
  • 6
  • 9
  • 1
    Adding the minus means that you are cutting the range from 2 until the end, so this just happens to work if you are dropping all columns to the left of an index but not if you are dropping one specific column in the middle. – Steven M. Mortimer Aug 18 '19 at 14:45