Here is an example of using cut
to break input into fields using a space delimiter, and obtaining the second field:
cut -f2 -d' '
How can the delimiter be defined as a tab, instead of a space?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityHere is an example of using cut
to break input into fields using a space delimiter, and obtaining the second field:
cut -f2 -d' '
How can the delimiter be defined as a tab, instead of a space?
Two ways:
Press Ctrl+V and then Tab to use "verbatim" quoted insert.
cut -f2 -d' ' infile
or write it like this to use ANSI-C quoting:
cut -f2 -d$'\t' infile
The $'...'
form of quotes isn't part of the POSIX shell language (not yet), but works at least in ksh, mksh, zsh and Busybox in addition to Bash.
-d
argument).
Nov 4, 2014 at 14:40
ksh93
, not bash
. That's supported by ksh93, zsh, bash, mksh and FreeBSD sh at least (it might make it to the next major reversion of the POSIX standard specification for sh
).
Oct 23, 2017 at 11:42
Tab is the default.
See the cut man page.
-d delim
Use delim as the field delimiter character instead of the tab
character.
So you can just write
cut -f 2
cut
for Windows would not follow the complete standard.
Apr 19, 2015 at 12:32
cut
for Windows and doesn't follow the POSIX specification for it, there is no reason to assume that any POSIX script will work with that system. Stick to POSIX-specified features. Don't try to allow for hypothetical future non-compliant implementations; that's not what "portability" means.
cut -f 2
is way more readable than cut -d' ' -f 2
or cut -d$'\t' -f 2
awk -F '\t' '{ print $2 }' inputfile
This extracts the second tab-delimited field of each line of input from inputfile
.
cut
. Moreover, cut(1)
is waaay faster than awk(1)
.
May 14, 2020 at 19:10
More generically, without requiring any invisible characters: Use tr
to convert the delimiters to a format that can be specified more easily to cut
.
$ echo -e "a\tb\tc" |tr '\t' ' ' |cut -d' ' -f2
b
tr
is a simple, but powerful, character matching and replacement tool.
abc(space)def(tab)ghi
? Your answer will yield def
, but it should yield ghi
. Similarly, if the input is ABC(tab)DEF(space)GHI
, your answer will yield DEF
, but it should yield DEF(space)GHI
.
Mar 26, 2019 at 20:26
echo -e "abc\tdef ghi" |tr '\t' ',' |cut -d',' -f2
Mar 26, 2019 at 22:29
Alternatively, one could wrap cut in a function.
function getColumns ()
{
local -r delimiter="${1:?}"
local -r columns="${2:?}"
if [[ "$delimiter" == '\t' || "$delimter" == "tab" ]]; then
cut "--fields=${columns}"
return
fi
cut "--delimiter=${delimiter}" "--fields=${columns}"
}
I use TAB
and cut
in these ways:
# quote the whole thing, use TAB escape
cut "-d\t" -f 2
# just quote the tab escape
cut -d "\t" -f 2
# Use Ctrl-V to quote Ctrl-I (TAB is Ctrl-I, see man ascii)
cut -d^V^I -f 2
"\t"
is interpreted by the shell, which does understand backslash-escapes, and $(...)
, and ... What's your $SHELL
or getent passwd $(id -u) | cut "-d:" -f6
. Also cat --show-all datafile | head -n 10
. Please edit your question to add whatever information you get. Do not use Add Comment.
Sep 2 at 23:15
printf '%s\t%s\t%s\n' 1 2 3 | cut -d "\t" -f 2
(will not extract the 2
in the 2nd column). Just use cut
without the -d
option (tab is the default delimiter).
cut
fails when given -d "\t"
or "-d\t"
: “cut: the delimiter must be a single character”.
Sep 6 at 10:29