How to delete the first column ( which is in fact row names) from a data file in linux?

前端 未结 5 1889
失恋的感觉
失恋的感觉 2021-01-01 11:10

I have data file with many thousands columns and rows. I want to delete the first column which is in fact the row counter. I used this command in linux:

cut          


        
5条回答
  •  既然无缘
    2021-01-01 11:45

    idiomatic use of cut will be

    cut -f2- input > output
    

    if you delimiter is tab ("\t").

    Or, simply with awk magic (will work for both space and tab delimiter)

     awk '{$1=""}1' input | awk '{$1=$1}1' > output
    

    first awk will delete field 1, but leaves a delimiter, second awk removes the delimiter. Default output delimiter will be space, if you want to change to tab, add -vOFS="\t" to the second awk.

    UPDATED

    Based on your updated input the problem is the initial spaces that cut treats as multiple columns. One way to address is to remove them first before feeding to cut

    sed 's/^ *//' input | cut -d" " -f2- > output
    

    or use the awk alternative above which will work in this case as well.

提交回复
热议问题