25

There is a file which the delimiter is tab ,when i use the command

cut -d \t file.txt  #or  "\t"  or  "\\t"

I get this message

cut: you must specify a list of bytes, characters, or fields

Try `cut --help' for more information.

How to use the cut command?

Community
  • 1
  • 1
showkey
  • 482
  • 42
  • 140
  • 295
  • 1
    The problem is not the delimiter, but the absence of fields or columns to cut. – Jonathan Leffler Jan 28 '13 at 02:53
  • If these aren't working, please show us a sample of the input file so we can compare. – Kyle Maxwell Jan 28 '13 at 03:07
  • 2
    The delimiter **is** "TAB" by default; however, if for some reason that's not working the way one would expect, specifying the "TAB" character in bash can be achived with: `cut -d$'\t'` or, perhaps, by hitting Ctrl-v then the TAB key--i.e. `cut -d 'CTRL-v TAB'`. **NOTE**: instead of simply closing this question, it might be prudent to move it to https://unix.stackexchange.com. **EDIT**: it seems duplicate already exists at http://unix.stackexchange.com/questions/35369/how-to-cut-by-tab-character – ILMostro_7 Aug 14 '15 at 00:42

3 Answers3

36

Cut splits the input lines at the given delimiter (-d, --delimiter).

To split by tabs omit the -d option, because splitting by tabs is the default.

By using the -f (--fields) option you can specify the fields you are interrested in.

echo -e "a\tb\tc" |cut -f 1 # outputs "a"
echo -e "a\tb\tc" |cut -f 2 # outputs "b"
echo -e "a\tb\tc" |cut -f 3 # outputs "c"
echo -e "a\tb\tc" |cut -f 1,3 # outputs "a\tc"
echo -e "a\tb\tc\td\te" |cut -f 2-4 # outputs "b\tc\td"

You can also specify the output delimiter (--output-delimiter) and get rid of lines not containing any delimiters (-s/--only-delimited)

echo -e "a\tb\tc\td\te" |cut -f 2-4 --output-delimiter=":" # outputs b:c:d

If you are interrested in the first field of your input file simply do...

cut -f 1 file.txt
mit
  • 11,083
  • 11
  • 50
  • 74
bikeshedder
  • 7,337
  • 1
  • 23
  • 29
19

Default delimiter is '\t' so you just need to execute:

cut -f <n> file.txt

Where <n> is the number of the column

OnaBai
  • 40,767
  • 6
  • 96
  • 125
2

You can try to place a tab between the quotes if you first press " v" then the "" key

eg  cat > test.txt 
a    b     c
<ctrl d>

$ grep test.txt | cut -f 2 -d "<ctrl v> <presstab>"

will return "b"