I want to use space as a delimiter with the cut command.
What syntax can I use for this?
cut -d ' ' -f 2 Where 2 is the field number of the space-delimited field you want.
cut, but you can with cuts which tries to "fix" all of cut limitations: github.com/arielf/cutscut -d ' ' -f 3,6,9,12,15,18 without having to specify every number?cuts sounds like it might be good.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.
tr translates or deletes characters. The -s option replaces repeats with a single occurrence.cut or tr examples to work. the awk example worked perfectly for what i was doing (splitting output with multiple spaces between columns)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.awkTo 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:
-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:
'...' is taken literally and forms a single argument"..." 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.'...' or "..." or unquoted \ instances) - thus, the command being invoked never sees the quote characters.--delimiter= all of this explanation is moot, especially in shell scripts.You can also say:
cut -d\ -f 2 Note that there are two spaces after the backslash.
\ 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.I just discovered that you can also use "-d ":
cut "-d " $ cat a hello how are you I am fine $ cut "-d " -f2 a how am 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 removalscut, 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.
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 lineAs 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.
manpage isn't enough. Let's take a look: "-d delimUsedelimas the field delimiter character instead of the tab character." (BSDcut, but the GNU version and the POSIX spec pretty much state the same). Using a shell to invokecut- the typical case - therefore requires you to know how to generally pass a space as an argument using shell syntax, which is arguably not thecutman page's job. Real-world examples always help, however, and the GNU man page lacks them.