Search code examples
cut

Can't cut column in Linux


I have a file like this

ATOM   3197 HD13 ILE   206       9.900  15.310  13.450  0.0196 1.4870
ATOM   3198  C   ILE   206      10.870  16.560  17.500  0.8343 1.9080
ATOM   3199  OXT ILE   206      11.780  15.734  17.425 -0.8190 1.6612
ATOM   3200  O   ILE   206       9.929  16.225  18.095 -0.8190 1.6612

I want to cut the second column, however when I use

cut -f1,3,4,5,6,7,8,9,10 filename

it doesn't work. Am I do something wrong?


Solution

  • This is because there are multiple spaces and cut can just handle them one by one.

    You can start from the 5th position:

    $ cut -d' ' -f 1,5- file
    ATOM HD13 ILE   206       9.900  15.310  13.450  0.0196 1.4870
    ATOM  C   ILE   206      10.870  16.560  17.500  0.8343 1.9080
    ATOM  OXT ILE   206      11.780  15.734  17.425 -0.8190 1.6612
    ATOM  O   ILE   206       9.929  16.225  18.095 -0.8190 1.6612
    

    Or squeeze spaces with tr -s like below (multiple spaces will be lost, though):

    $ tr -s ' ' < file | cut -d' ' -f1,3,4,5,6,7,8,9,10
    ATOM HD13 ILE 206 9.900 15.310 13.450 0.0196 1.4870
    ATOM C ILE 206 10.870 16.560 17.500 0.8343 1.9080
    ATOM OXT ILE 206 11.780 15.734 17.425 -0.8190 1.6612
    ATOM O ILE 206 9.929 16.225 18.095 -0.8190 1.6612
    

    Note you can indicate from 3 to the end with 3-:

    tr -s ' ' < file | cut -d' ' -f1,3-
    

    In fact I would use awk for this:

    awk '{$2=""; print}' file
    

    or just

    awk '{$2=""} 1' file