move_slow_break_things
move_slow_break_things

Reputation: 847

Remove particular lines from a text file in R

I used sink() to write the output of my script to a .txt file but for whatever reason the prompt got written to the txt file too. I asked a question a few days ago to fix that but no answer so here I am approaching it from a different angle. Here is the txt file that got generated by my script:

> geno(hdr) 
DataFrame with 12 rows and 3 columns
            Number        Type                         Description
       <character> <character>                         <character>
GT               1      String                            Genotype
GQ               1     Integer                    Genotype Quality
DP               1     Integer                          Read Depth
HDP              2     Integer                Haplotype Read Depth
HQ               2     Integer                   Haplotype Quality
...            ...         ...                                 ...
mRNA             .      String                     Overlaping mRNA
rmsk             .      String                  Overlaping Repeats
segDup           .      String Overlaping segmentation duplication
rCov             1       Float                   relative Coverage
cPd              1      String                called Ploidy(level)

> sink()

Since the lines I want gone are the ones that start with >, I was thinking if there was a way to open the txt file and remove those particular lines. In this case, the lines > geno(hdr) and > sink() would be removed. I am not good at regex at all in R so I don't know how this would work. Any help appreciated. Thanks.

Upvotes: 2

Views: 3808

Answers (1)

akuiper
akuiper

Reputation: 214967

You can try this. Read the file line by line and check if the line starts with >, if not, append it to a new file:

con <- file('test.txt', open = 'r')
while(TRUE) {
    line <- readLines(con, n = 1)
    if(length(line) == 0) break
    else if(!startsWith(line, ">")){
        write(line, file = "newTest.txt", append = TRUE)
    } 
}

Upvotes: 2

Related Questions