Question

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?

Was it helpful?

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

OTHER TIPS

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**

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top