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

前端 未结 5 1908
失恋的感觉
失恋的感觉 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:34

    As @karakfa notes, it looks like it's the leading whitespace which is causing your issues.

    Here's a sed oneliner to do the job (that will account for spaces or tabs):

    sed -i.bak "s|^[ \t]\+[0-9]\+[ \t]\+||" input.txt
    

    Explanation:

    -i       edit existing file in place
    .bak     backup original file and add .bak file extension (can use whatever you like)
    
    s        substitute
    |        separator (easiest character to read as sed separator IMO)
    ^        start match at start of the line
    [ \t]    match space or tab
    \+       match one or more times (escape required so sed does not interpret '+' literally)
    [0-9]    match any number 0 - 9
    

    As noted; the input.txt file will be edited in place. The original content of input.txt will be saved as input.txt.bak. Use just -i instead if you don't want a backup of the original file.

    Also, if you know that they are definitely leading spaces (not tabs), you could shorten it to this:

    sed -i.bak "s|^ \+[0-9]\+[ \t]\+||" input.txt
    

提交回复
热议问题