I want to use space as a delimiter with the cut
command.
What syntax can I use for this?
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.
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}'
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:
-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 \<space> # <space> used to represent an actual space for technical reasons
With approach (d), all of the following forms are EQUIVALENT:
-d' '
-d" "
"-d "
'-d '
d\<space>
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:
'...'
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 \
instances) - thus, the command being invoked never sees the quote characters.You can also say
cut -d\ -f 2
note that there are two spaces after the backslash.
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
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.
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
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.