440

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

What syntax can I use for this?

3
  • 3
    @UncleZeiv Got it; thanks for clarifying; given the interest in this question, it's fair to assume that the man page isn't enough. Let's take a look: "-d delim Use delim as the field delimiter character instead of the tab character." (BSD cut, but the GNU version and the POSIX spec pretty much state the same). Using a shell to invoke cut - the typical case - therefore requires you to know how to generally pass a space as an argument using shell syntax, which is arguably not the cut man page's job. Real-world examples always help, however, and the GNU man page lacks them. Commented May 5, 2015 at 20:27
  • 4
    although the selected answer is technically correct, consider selecting the more recent and comprehensive answer by @mklement0 as the canonical answer so that it filters to the top. Commented Sep 16, 2015 at 18:34
  • BeniBela's answer is way more concise and what most situations would call for. Commented Sep 5 at 21:03

8 Answers 8

481
cut -d ' ' -f 2 

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

Sign up to request clarification or add additional context in comments.

6 Comments

can you tell cut to use any number of a certain character as the delimiter, like in RegEx? e.g. any number of spaces, e.g. \s+
@foampile No, I don't believe you can.
You can't use regexes with cut, but you can with cuts which tries to "fix" all of cut limitations: github.com/arielf/cuts
can you get every third space-delimted field? like cut -d ' ' -f 3,6,9,12,15,18 without having to specify every number?
It's such a common use case to split on variable number of contiguous space it's a bit funny it's not dealt with - but following up on cuts sounds like it might be good.
|
280

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.

5 Comments

Yes! This should be the accepted answer, or at least included in the accepted answer. I can't remember ever trying to use cut on space separated data when I didn't have to normalize the spaces.
This is money. tr translates or deletes characters. The -s option replaces repeats with a single occurrence.
i'm on a mac and i couldn't get the cut or tr examples to work. the awk example worked perfectly for what i was doing (splitting output with multiple spaces between columns)
@Alf47 It's unintuitive, given how most programs work, but tr is a "filter" and only receives input on stdin and only writes to stdout. It doesn't understand filename arguments, and you may not even get an error if you try to pass it a filename. This is just one of those things you learn. Your cut and tr should work basically the same as on Linux, or other Unices, so I'd encourage you to keep tinkering, and have a look at the EXAMPLES section of the man pages.
Yes, just use awk
63

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.

6 Comments

For cut from Gow only the option with double quote work: -d" ", -d " ", "-d ". All options with single quote or <space> not work.
@Frank, yes, it is the shell that matters with respect to the quoting styles supported, and given that Gow runs on Windows, you either need to use cmd.exe's syntax ("-quoting only, ^ as the escape char.) or PowerShell's syntax (` as the escape char.)
With --delimiter= all of this explanation is moot, especially in shell scripts.
@mklement0 The MacOS is the least of most people's concerns doing shell programming. Using the edge case as the crux of your argument should bring into question why are you using proprietary tools. By the way, plenty of people run VirtualBox on the MacOS, so --delimiter= might be what they want as they do development in Linux.
It is also tagged as bash, and that is a GNU tool. Maybe install GNU tools and call it a day?
|
49

You can also say:

cut -d\ -f 2 

Note that there are two spaces after the backslash.

6 Comments

The person who knows that '\' escapes the next character would be very careful to note what came next. Using '\' to escape space characters like this is a very common idiom.
@Jonathan Hartley commonly most of the codes are unreadable indeed :)
From a linux/unix perspective, \ was my first attempt and it worked. I agree it is less obvious when compared to ' ', but I'm sure many are glad to read it here as reassurance of behavior. For a better understanding, please see @mklement0's comment below.
@JonathanHartley correction: "the selfish person who knows that '\' escapes the next character and assumes everybody else knows that also". For personal projects this does not apply, but in a team-setting, that assumption is a very dangerous (and potentially costly) one.
@EduardNicodei Oh I agree. We were talking about readers of the code ("who notices...?"), not authors. But also, on some teams it's fine to assume a certain level of proficiency. Depends on the environment.
|
9

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 

3 Comments

Note that from cut's perspective all of the following are identical: "-d ", '-d ', -d" ", -d' ', and -d\<space>: all forms directly append the option argument (a space) to the option (-d) and result in the exact same string by the time cut sees them: a single argument containing d followed by a space, after the shell has performed quote removal
@mklement0's answer should be the answer. It is the most comprehensive on this page (even though it is a comment).
@QZSupport: I appreciate the sentiment and the encouragement - it has inspired me to post my own answer with additional background information.
5

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 

Comments

3

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.

Comments

1

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.

1 Comment

I'm glad you explained your reasoning; indeed this is confusing for someone who does not know sed, and this may inspire them to learn more. And you explain the sed magic very well, which is quite helpful.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.