355

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?

ilkkachu
  • 133,243
  • 15
  • 236
  • 397
Muhammad Hasan Khan
  • 3,653
  • 2
  • 14
  • 6

6 Answers6

493

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.

ilkkachu
  • 133,243
  • 15
  • 236
  • 397
Birei
  • 7,924
  • 2
  • 22
  • 14
310

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
Mikel
  • 56,387
  • 13
  • 130
  • 149
  • 4
    But it is probably always safer to mention such flags explicitly, for both readability and portability. I can imagine some people designing a `cut` for Windows would not follow the complete standard. – Willem Van Onsem Apr 19 '15 at 12:32
  • 5
    This should be accepted answer: simplest code, simplest explanation. ya nailed it: https://www.youtube.com/watch?v=dpNTHl7y45Y – neuronet Jun 16 '16 at 21:28
  • 5
    @WillemVanOnsem, if someone writes a version of `cut` for Windows and doesn't follow the [POSIX specification](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/cut.html) 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. – Wildcard Mar 26 '19 at 20:37
  • @WillemVanOnsem Additionally, `cut -f 2` is way more readable than `cut -d' ' -f 2` or `cut -d$'\t' -f 2` – Mouradif Jun 21 '19 at 10:10
19
awk -F '\t' '{ print $2 }' inputfile

This extracts the second tab-delimited field of each line of input from inputfile.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
Iľja
  • 429
  • 2
  • 3
11

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.

Brent Bradburn
  • 1,246
  • 1
  • 13
  • 23
  • 1
    But what if the input is `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``. – G-Man Says 'Reinstate Monica' Mar 26 '19 at 20:26
  • @G-Man: The space delimiter was only an example. Use whatever delimiter is appropriate for your data -- a comma for example. `echo -e "abc\tdef ghi" |tr '\t' ',' |cut -d',' -f2` – Brent Bradburn Mar 26 '19 at 22:29
  • 1
    ...But, yeah, if the delimiter must be a tab, then my approach won't work. – Brent Bradburn Mar 27 '19 at 00:04
0

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}" 
}
-1

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
waltinator
  • 4,439
  • 1
  • 16
  • 21
  • The `cut` utility does not understand the `\t` as tab. – Kusalananda Aug 15 '23 at 15:26
  • @Kusalananda Show the failure. By quoting , the `"\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. – waltinator Sep 02 '23 at 23:15