Sorry if this is a question more on regular expressions. I am dealing with several files which have been badly named. For example the files are given either the extensions txt, TXT or Txt. I wish to select all those files ending with 'txt' ignoring case. Here is how I would do it in bash (Redhat FC2) : touch a1.txt a2.TXT a3.Txt txt.control TXT.control ls -1 | grep -i "txt$" Here is how I am currently doing it in R a <- list.files(all.files=T) grep( "txt$", a, ignore.case=T, value=T ) Is it possible for me to modify the following line to include ignore case option ? a <- list.files( pattern="txt$" ) Thank you. Regards, Adai
list files ignoring the case option
5 messages · Sundar Dorai-Raj, Brian Ripley, Bert Gunter +1 more
Adaikalavan Ramasamy wrote:
Sorry if this is a question more on regular expressions. I am dealing with several files which have been badly named. For example the files are given either the extensions txt, TXT or Txt. I wish to select all those files ending with 'txt' ignoring case. Here is how I would do it in bash (Redhat FC2) : touch a1.txt a2.TXT a3.Txt txt.control TXT.control ls -1 | grep -i "txt$" Here is how I am currently doing it in R a <- list.files(all.files=T) grep( "txt$", a, ignore.case=T, value=T ) Is it possible for me to modify the following line to include ignore case option ? a <- list.files( pattern="txt$" ) Thank you. Regards, Adai
Not much of a regexpr guy myself, but the following should work: list.files(pattern = "[tT][xX][tT]$") There's probably a better answer though. --sundar
On Thu, 4 Nov 2004, Adaikalavan Ramasamy wrote:
Sorry if this is a question more on regular expressions. I am dealing with several files which have been badly named. For example the files are given either the extensions txt, TXT or Txt. I wish to select all those files ending with 'txt' ignoring case. Here is how I would do it in bash (Redhat FC2) : touch a1.txt a2.TXT a3.Txt txt.control TXT.control ls -1 | grep -i "txt$" Here is how I am currently doing it in R a <- list.files(all.files=T) grep( "txt$", a, ignore.case=T, value=T )
I'd write that in one line, but it seems as good a way as any.
Is it possible for me to modify the following line to include ignore case option ? a <- list.files( pattern="txt$" )
Not as such.
First, I think you want "\\.txt$" there if you do mean file extensions.
You can use a regexp that ignores case, though, e.g. "\\.[Tt]{Xx][Tt]".
But I would just use your original idea, which is essentially what ls() is
doing internally and is self-documenting.
Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Simpler:First get uniform case. ?toupper or ?tolower -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box
-----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Sundar Dorai-Raj Sent: Thursday, November 04, 2004 6:39 AM To: ramasamy at cancer.org.uk Cc: R-help Subject: Re: [R] list files ignoring the case option Adaikalavan Ramasamy wrote:
Sorry if this is a question more on regular expressions. I
am dealing
with several files which have been badly named. For example
the files
are given either the extensions txt, TXT or Txt. I wish to
select all
those files ending with 'txt' ignoring case. Here is how I would do it in bash (Redhat FC2) : touch a1.txt a2.TXT a3.Txt txt.control TXT.control ls -1 | grep -i "txt$" Here is how I am currently doing it in R a <- list.files(all.files=T) grep( "txt$", a, ignore.case=T, value=T ) Is it possible for me to modify the following line to include ignore case option ? a <- list.files( pattern="txt$" ) Thank you. Regards, Adai
Not much of a regexpr guy myself, but the following should work: list.files(pattern = "[tT][xX][tT]$") There's probably a better answer though. --sundar
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Thanks to Sundar Dorai-Raj, Prof. Ripley and Berton Gunter for the solution. I think I will take Prof. Ripley's suggestion and stick with my initial solution for code readability but I am sure the regexp stuff will come handy next time.
On Thu, 2004-11-04 at 15:10, Prof Brian Ripley wrote:
On Thu, 4 Nov 2004, Adaikalavan Ramasamy wrote:
Sorry if this is a question more on regular expressions. I am dealing with several files which have been badly named. For example the files are given either the extensions txt, TXT or Txt. I wish to select all those files ending with 'txt' ignoring case. Here is how I would do it in bash (Redhat FC2) : touch a1.txt a2.TXT a3.Txt txt.control TXT.control ls -1 | grep -i "txt$" Here is how I am currently doing it in R a <- list.files(all.files=T) grep( "txt$", a, ignore.case=T, value=T )
I'd write that in one line, but it seems as good a way as any.
Is it possible for me to modify the following line to include ignore case option ? a <- list.files( pattern="txt$" )
Not as such.
First, I think you want "\\.txt$" there if you do mean file extensions.
You can use a regexp that ignores case, though, e.g. "\\.[Tt]{Xx][Tt]".
But I would just use your original idea, which is essentially what ls() is
doing internally and is self-documenting.