matlabcat
matlabcat

Reputation: 192

How can I remove specified characters from a text file in r?

I am trying to compare two text files in R using diffr, but I know that one of the files I am comparing has a carriage return and curly brace '{' at the top of the text file and also a '-}' at the bottom. How can I remove the carriage return and unwanted characters before my comparison?

I can read the text within the file using:

test<-readLines("C:\\Users\\examplefile.txt")

But after that I am unsure? Do I have to rewrite the file? I don't think I can use 'pattern' to find what I want to delete as it's in two separate locations in the text?

In 'new.txt' I have this:

{ 
:some text here
-}

And in 'old.txt' I have:

:some text here

I'm using:

library(diffr)
diffr("old.txt","new.txt") to read the differences, but how do I extract the carriage return and braces first?

Upvotes: 0

Views: 674

Answers (1)

GKi
GKi

Reputation: 39667

You can use head and tail to remove the first two lines and last two lines.

new.txt <- tail(head(new.txt, -2), -2)

new.txt == old.txt
#[1] TRUE

Data:

new.txt <- readLines(textConnection("
{ 
:some text here
-}
"))

old.txt <- readLines(textConnection(":some text here"))

Upvotes: 1

Related Questions