Remove all text before colon

后端 未结 9 1175
天命终不由人
天命终不由人 2020-11-27 12:45

I have a file containing a certain number of lines. Each line looks like this:

TF_list_to_test10004/Nus_k0.345_t0.1_         


        
相关标签:
9条回答
  • 2020-11-27 13:43

    Here are two ways of doing it in R:

    foo <- "TF_list_to_test10004/Nus_k0.345_t0.1_e0.1.adj:PKMYT1"
    
    # Remove all before and up to ":":
    gsub(".*:","",foo)
    
    # Extract everything behind ":":
    regmatches(foo,gregexpr("(?<=:).*",foo,perl=TRUE))
    
    0 讨论(0)
  • 2020-11-27 13:44

    Using sed:

    sed 's/.*://' < your_input_file > output_file
    

    This will replace anything followed by a colon with nothing, so it'll remove everything up to and including the last colon on each line (because * is greedy by default).

    As per Josh O'Brien's comment, if you wanted to only replace up to and including the first colon, do this:

    sed "s/[^:]*://"
    

    That will match anything that isn't a colon, followed by one colon, and replace with nothing.

    Note that for both of these patterns they'll stop on the first match on each line. If you want to make a replace happen for every match on a line, add the 'g' (global) option to the end of the command.

    Also note that on linux (but not on OSX) you can edit a file in-place with -i eg:

    sed -i 's/.*://' your_file
    
    0 讨论(0)
  • 2020-11-27 13:49

    If you have GNU coreutils available use cut:

    cut -d: -f2 infile
    
    0 讨论(0)
提交回复
热议问题