Jaelebi
Jaelebi

Reputation: 6119

Use space as a delimiter with cut command

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

What syntax can I use for this?

Upvotes: 434

Views: 467440

Answers (8)

BeniBela
BeniBela

Reputation: 16907

Usually if you use space as delimiter, you want to treat multiple spaces as one, because you parse the output of a command aligning some columns with spaces. (and the google search for that lead me here)

In this case a single cut command is not sufficient, and you need to use:

tr -s ' ' | cut -d ' ' -f 2

Or

awk '{print $2}'

This works because AWK's default input field separator is one or more whitespace characters; in regex terms, it's something like [ \t]+. The AWK solution has the added benefit of transparently handling leading/trailing spaces on the data row, whereas the tr + cut solution does not.

Upvotes: 267

mklement0
mklement0

Reputation: 437176

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.

Note: The GNU implementation of cut, as found on many Linux distros by default, supports --delimiter as a more descriptive alias of -d. The same considerations apply as for -d, except that directly attaching the option-argument requires use of = as the separator, whereas no separator is used with -d; e.g.,
echo 'one two' | cut --delimiter=' ' -f 1 vs. echo 'one two' | cut -d' ' -f 1

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 \ (\<space> is an escaped space to be used literally)
  • 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 - then without quotes or \ prefix!.

  • (d): cut sees -d plus a space char - then 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 (any enclosing '...' or "..." or unquoted \ instances) - thus, the command being invoked never sees the quote characters.

Upvotes: 62

Chas. Owens
Chas. Owens

Reputation: 64909

You can also say:

cut -d\  -f 2

Note that there are two spaces after the backslash.

Upvotes: 49

Anssi
Anssi

Reputation: 2847

You can't do it easily with cut if the data has for example multiple spaces. I have found it useful to normalize input for easier processing. One trick is to use sed for normalization as below.

echo -e "foor\t \t bar" | sed 's:\s\+:\t:g' | cut -f2  #bar

Upvotes: 5

Stephen Quan
Stephen Quan

Reputation: 25891

I have an answer (I admit somewhat confusing answer) that involvessed, regular expressions and capture groups:

  • \S* - first word
  • \s* - delimiter
  • (\S*) - second word - captured
  • .* - rest of the line

As a sed expression, the capture group needs to be escaped, i.e. \( and \).

The \1 returns a copy of the captured group, i.e. the second word.

$ echo "alpha beta gamma delta" | sed 's/\S*\s*\(\S*\).*/\1/'
beta

When you look at this answer, its somewhat confusing, and, you may think, why bother? Well, I'm hoping that some, may go "Aha!" and will use this pattern to solve some complex text extraction problems with a single sed expression.

Upvotes: 1

Harry Mangalam
Harry Mangalam

Reputation: 51

scut, a cut-like utility (smarter but slower I made) that can use any perl regex as a breaking token. Breaking on whitespace is the default, but you can also break on multi-char regexes, alternative regexes, etc.

scut -f='6 2 8 7' < input.file  > output.file

so the above command would break columns on whitespace and extract the (0-based) cols 6 2 8 7 in that order.

Upvotes: 3

fedorqui
fedorqui

Reputation: 289515

I just discovered that you can also use "-d ":

cut "-d "

Test

$ cat a
hello how are you
I am fine
$ cut "-d " -f2 a
how
am

Upvotes: 8

RichieHindle
RichieHindle

Reputation: 281405

cut -d ' ' -f 2

Where 2 is the field number of the space-delimited field you want.

Upvotes: 478

Related Questions