[R] Delete Comment Lines from SQL String as a Vector

jim holtman jholtman at gmail.com
Tue Feb 22 01:43:19 CET 2011


Here is the way I usually write my SQL:

> x <- c('
+ select comm, count(*)   -- count the number of comms
+    from myDF    -- this is the dataframe
+    group by comm  -- grouping argument
+ ')
> x  # as vector
[1] "\nselect comm, count(*)   -- count the number of comms\n   from
myDF    -- this is the dataframe\n   group by comm  -- grouping
argument\n"
> cat(x)  # nicely printed

select comm, count(*)   -- count the number of comms
   from myDF    -- this is the dataframe
   group by comm  -- grouping argument
> # remove comments
> y <- gsub("--[^\n]*", "", x)
> y
[1] "\nselect comm, count(*)   \n   from myDF    \n   group by comm  \n"
> cat(y)

select comm, count(*)
   from myDF
   group by comm
>


On Mon, Feb 21, 2011 at 6:08 PM, Mai Dang <mdmining at gmail.com> wrote:
> Hi,
> I tried to remove the text starts by "--" to the end of the line as below
>
>
> sql=c("-- This is a comment line",
>         "select sysdate  -- This is a comment Text" ,
>         " from dual ")
>>sql
> [1] "-- This is a comment line"
> [2] "select sysdate  -- This is a comment Text"
> [3] " from dual "
>
>
> I try to have this
>> sql
> [1] "select sysdate "
> [2] " from dual "
>
> Thank You for your help,
> Mai Dang
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?



More information about the R-help mailing list