hel hel - 2 months ago 5x
Linux Question

How to use cut for showing the first field of a file

Below is my data file sname , I use command

cut -f1 sname
to output Number in that file. But it outputs Name field at the same time.

11 Vivek
12 Renuka
13 Prakash

Separator is

EDIT: As @fancyPants points out, I find in my .vimrc
set tabstop=8
. That's the problem.
cut -f1 sname


The default separator for cut is a space, not tabulator.

You can specify it either by pressing CTRL-v Tab

cut -f1 -d'   ' sname

or this way:

cut -f1 -d$'\t' sname

Correction: The default delimiter for cut is tab, so this is not necessary.

Edit: When this doesn't work, you don't have tabs there. You can check for tabs when you look at the file with vi. Then you enter these commands:

:set list

for a tab you should see ^I or to make it more clear you can use

:set listchars=tab:>-

Then the tabs will show as >---.


I think your problem is, that you have multiple whitespaces. When you don't want to use awk, which is surely easier, this would be an alternative:

sed 's/ \+/ /g' sname | cut -f2 -d" "

sed replaces multiple whitespaces with one.