[R] help using SED

peter dalgaard pdalgd at gmail.com
Sun Dec 6 10:28:26 CET 2015


> On 06 Dec 2015, at 04:53 , David Winsemius <dwinsemius at comcast.net> wrote:
> 
>> 
>> On Dec 5, 2015, at 4:49 PM, Glenn Schultz <glennmschultz at me.com> wrote:
>> 
>> All,
>> 
>> I have not used SED in the past so I am continuing to read its documentation but I need some help with R
>> 
>> Here is my SED command which works:
>>  system("sed -i ''  '/^[\r#]/d; /AGENCY/d' /Library/Frameworks/R.framework/Versions/3.2/Resources/library/BondLab/Temp_CashFlow/blx_test.cfm")
>> 
>> Here are my R commands which do not work.  
>> ReadCF <- paste("system(sed -i '' '/^[\r#]/d; /AGENCY/d' ", 
>> system.file(package = "BondLab"),"/Temp_CashFlow/", 
>> CashFlowData, ".cfm)", sep ="")
>> 
>> eval(parse(text = ReadCF))
>> I have managed to determine that the problem is the missing quotes but I do not know how to get those included in the below R command.
> 
> I see no explanation of what you are attempting, but the method of including an actual quote in an R character vector element is to escape it:
> 
>> cat("\"")
> "
>> nchar("\"")
> [1] 1
> 

At any rate, eval(parse(... is just silly (fortune(181); fortune(106)). 

Just use paste()  or --- probably better ---sprintf() to construct the command, and then do

cmd <- ....
system(cmd)

A further advantage is that you can cat(cmd, \n)  before running it and see whether it contains what you intended.

I think (but you check) that this works:

fmt <- "sed -i ''  '/^[\r#]/d; /AGENCY/d' %s/Temp_CashFlow/%s.cfm"
cmd <- sprintf(fmt, system.file(package = "BondLab"), CashFlowData)
system(cmd)

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list