Hello,
Can someone help me to write a Ruby one-liner to remove all the files
except one in a list of files? I have a list of pages of an original
PDF. Each page has been converted to a PNG file. They're each numbered
with an underscore and a number at their end.
filename_1.png
filename_2.png
filename_3.png
...
I need to basically delete all of the files except for the first one,
the one with the "_1" suffix at the end. Can I do that wiith a
one-liner?
Hello,
Can someone help me to write a Ruby one-liner to remove all the files
except one in a list of files? I have a list of pages of an original
PDF. Each page has been converted to a PNG file. They're each numbered
with an underscore and a number at their end.
filename_1.png
filename_2.png
filename_3.png
...
I need to basically delete all of the files except for the first one,
the one with the "_1" suffix at the end. Can I do that wiith a
one-liner?
Hello,
Can someone help me to write a Ruby one-liner to remove all the files
except one in a list of files? I have a list of pages of an original
PDF. Each page has been converted to a PNG file. They're each numbered
with an underscore and a number at their end.
filename_1.png
filename_2.png
filename_3.png
...
I need to basically delete all of the files except for the first one,
the one with the "_1" suffix at the end. Can I do that wiith a
one-liner?
On Tue, Jan 8, 2013 at 9:48 PM, Peter Bailey <lists@ruby-forum.com> wrote:
Hello,
Can someone help me to write a Ruby one-liner to remove all the files
except one in a list of files? I have a list of pages of an original
PDF. Each page has been converted to a PNG file. They're each numbered
with an underscore and a number at their end.
filename_1.png
filename_2.png
filename_3.png
...
I need to basically delete all of the files except for the first one,
the one with the "_1" suffix at the end. Can I do that wiith a
one-liner?
On Wed, Jan 9, 2013 at 4:09 PM, Robert Klemme <shortcutter@googlemail.com> wrote:
On Tue, Jan 8, 2013 at 9:48 PM, Peter Bailey <lists@ruby-forum.com> wrote:
Hello,
Can someone help me to write a Ruby one-liner to remove all the files
except one in a list of files? I have a list of pages of an original
PDF. Each page has been converted to a PNG file. They're each numbered
with an underscore and a number at their end.
filename_1.png
filename_2.png
filename_3.png
...
I need to basically delete all of the files except for the first one,
the one with the "_1" suffix at the end. Can I do that wiith a
one-liner?
There's certainly many ways to do this, but globing will ignore the dot
dirs, and is a bit shorter.
File.delete *Dir['*.png'].reject {|f| f =~ /_1\.png$/ }
···
On 01/08/2013 05:04 PM, Damián M. González wrote:
Peter Bailey wrote in post #1091502:
Hello,
Can someone help me to write a Ruby one-liner to remove all the files
except one in a list of files? I have a list of pages of an original
PDF. Each page has been converted to a PNG file. They're each numbered
with an underscore and a number at their end.
filename_1.png
filename_2.png
filename_3.png
...
I need to basically delete all of the files except for the first one,
the one with the "_1" suffix at the end. Can I do that wiith a
one-liner?
There's certainly many ways to do this, but globing will ignore the dot
dirs, and is a bit shorter.
File.delete *Dir['*.png'].reject {|f| f =~ /_1\.png$/ }
Thanks, Brian. But, even preceding this with "ruby -e" gives me nothing.
Should I make this part of a script instead of it being a one-liner?
No, because here we are using the glob facility to do all the
filtering. In the other solution, there was the array returned from
the glob that was being filtered, and so the order was important for
the solution that removed the first entry in the array.
Jesus.
···
On Wed, Jan 9, 2013 at 5:00 PM, Peter Bailey <lists@ruby-forum.com> wrote:
"Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> wrote in post #1091607:
On Wed, Jan 9, 2013 at 4:09 PM, Robert Klemme >> <shortcutter@googlemail.com> wrote:
...
I need to basically delete all of the files except for the first one,
the one with the "_1" suffix at the end. Can I do that wiith a
one-liner?
That's odd... Works for me under 1.8.7, 1.9.2 and 1.9.3.
ruby -e "File.delete *Dir['*.png'].reject {|f| f=~ /_1\.png$/ }"
There will be no output. It will just delete the files.
There's certainly many ways to do this, but globing will ignore the dot
dirs, and is a bit shorter.
File.delete *Dir['*.png'].reject {|f| f =~ /_1\.png$/ }
Thanks, Brian. But, even preceding this with "ruby -e" gives me nothing.
Should I make this part of a script instead of it being a one-liner?
Excellent. Thanks. Hans is right, though. Dir.glob is unsorted. Is that
a problem here?
No, because here we are using the glob facility to do all the
filtering. In the other solution, there was the array returned from
the glob that was being filtered, and so the order was important for
the solution that removed the first entry in the array.
Excellent. Thanks. Hans is right, though. Dir.glob is unsorted. Is that
a problem here?
No, because here we are using the glob facility to do all the
filtering. In the other solution, there was the array returned from
the glob that was being filtered, and so the order was important for
the solution that removed the first entry in the array.
Jesus.
Beautiful. Thanks a lot.
This is what I'm getting. I'm sure i'm missing something simple.
Excellent. Thanks. Hans is right, though. Dir.glob is unsorted. Is that
a problem here?
No, because here we are using the glob facility to do all the
filtering. In the other solution, there was the array returned from
the glob that was being filtered, and so the order was important for
the solution that removed the first entry in the array.
Jesus.
Beautiful. Thanks a lot.
This is what I'm getting. I'm sure i'm missing something simple.