1

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?

Ooker
  • 1,969
  • 4
  • 28
  • 58
  • 2
    possible duplicate of [linux cut help - how to specify more spaces for the delimiter?](http://stackoverflow.com/questions/7142735/linux-cut-help-how-to-specify-more-spaces-for-the-delimiter) – devnull May 13 '14 at 12:30

3 Answers3

1

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
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • Wow, it does work. However is there any way to keep the format of others column for keeping it clearly? – Ooker May 13 '14 at 12:36
  • Yes, sure, see the option with `cut -d' ' -f 1,5- file` or `awk` way. – fedorqui May 13 '14 at 12:38
  • I have a question. Why does it not similar to 'cut -f1,3,4,5,6,7,8,9,10 filename'? – Ooker May 13 '14 at 12:51
  • Because there are multiple spaces between the columns, so that the 2nd will be empty, the 3rd another one, etc. You can see it by printing one column at a time: `cut -f3 file`, `cut -f4 file`, etc. – fedorqui May 13 '14 at 12:53
  • actually it doesn't cut the first 999 lines. I don't know what's wrong. – Ooker May 13 '14 at 12:56
  • Are you using windows files? Sometimes they screw files. Check with `cat -vte file`. – fedorqui May 13 '14 at 13:00
  • no, they are pure Linux files. Is it because starting from 1000, there are four characters? – Ooker May 13 '14 at 13:01
  • Aaaah yes, must be this, @Ooker . Then it is best to use the `awk` approach, not to be format-dependent. – fedorqui May 13 '14 at 13:30
1

There are many spaces in your file. So, you've to start with number of spaces.

The new.txt contains

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

and this is the command to print second column

root@52:/home/ubuntu# cut -d' ' -f4 new.txt

3197
3198
3199
3200

where -d stands for delimiter i.e 'space' in this case denoted by ' '

However, awk comes pretty handy in such cases

**# awk '{print $2}' new.txt**
Rich Benner
  • 7,873
  • 9
  • 33
  • 39
Himanshu Chauhan
  • 812
  • 9
  • 11
0

You can select the position of the content in the first row at that column (3197) and then select the string at the same position in all rows with awk:

cat filename | awk -v field="3197" 'NR==1 {c = index($0,field)} {print substr($0,c,length(field))}'

souce: https://unix.stackexchange.com/a/491770/20661

rubo77
  • 19,527
  • 31
  • 134
  • 226