Use space as a delimiter with cut command

前端 未结 8 1175
[愿得一人]
[愿得一人] 2020-12-07 07:24

I want to use space as a delimiter with the cut command.

What syntax can I use for this?

8条回答
  •  一个人的身影
    2020-12-07 08:05

    To complement the existing, helpful answers; tip of the hat to QZ Support for encouraging me to post a separate answer:

    Two distinct mechanisms come into play here:

    • (a) whether cut itself requires the delimiter (space, in this case) passed to the -d option to be a separate argument or whether it's acceptable to append it directly to -d.

    • (b) how the shell generally parses arguments before passing them to the command being invoked.

    (a) is answered by a quote from the POSIX guidelines for utilities (emphasis mine)

    If the SYNOPSIS of a standard utility shows an option with a mandatory option-argument [...] a conforming application shall use separate arguments for that option and its option-argument. However, a conforming implementation shall also permit applications to specify the option and option-argument in the same argument string without intervening characters.

    In other words: In this case, because -d's option-argument is mandatory, you can choose whether to specify the delimiter as:

    • (s) EITHER: a separate argument
    • (d) OR: as a value directly attached to -d.

    Once you've chosen (s) or (d), it is the shell's string-literal parsing - (b) - that matters:

    • With approach (s), all of the following forms are EQUIVALENT:

      • -d ' '
      • -d " "
      • -d \ # used to represent an actual space for technical reasons
    • With approach (d), all of the following forms are EQUIVALENT:

      • -d' '
      • -d" "
      • "-d "
      • '-d '
      • d\

    The equivalence is explained by the shell's string-literal processing:

    All solutions above result in the exact same string (in each group) by the time cut sees them:

    • (s): cut sees -d, as its own argument, followed by a separate argument that contains a space char - without quotes or \ prefix!.

    • (d): cut sees -d plus a space char - without quotes or \ prefix! - as part of the same argument.

    The reason the forms in the respective groups are ultimately identical is twofold, based on how the shell parses string literals:

    • The shell allows literal to be specified as is through a mechanism called quoting, which can take several forms:
      • single-quoted strings: the contents inside '...' is taken literally and forms a single argument
      • double-quoted strings: the contents inside "..." also forms a single argument, but is subject to interpolation (expands variable references such as $var, command substitutions ($(...) or `...`), or arithmetic expansions ($(( ... ))).
      • \-quoting of individual characters: a \ preceding a single character causes that character to be interpreted as a literal.
    • Quoting is complemented by quote removal, which means that once the shell has parsed a command line, it removes the quote characters from the arguments (enclosing '...' or "..." or \ instances) - thus, the command being invoked never sees the quote characters.

提交回复
热议问题