Trimming some string using ruby

Hi guys,
         I have the following information in a text file, which looks
like this:

-rw-rw-r-- 1 user user 12203 Aug 6 01:02 app1.sql
-rw-rw-r-- 1 user user 12343 Aug 6 01:02 app2.sql
-rw-rw-r-- 1 user user 12238 Aug 6 01:02 app3.sql

I need to Trim the unwanted strings and my output should look like this:

app1.sql
app2.sql
app3.sql

I am not good at string functions...let me know if anyone can help me
out!!

cheers

···

--
Posted via http://www.ruby-forum.com/.

file = File.open("foo")
filenames = file.map do |line|
  line.chomp.split(/ +/,9)[-1]
end
p filenames

Jayanth

···

On Thu, Aug 6, 2009 at 12:01 PM, Shekar Ls <idealone5@hotmail.com> wrote:

Hi guys,
        I have the following information in a text file, which looks
like this:

-rw-rw-r-- 1 user user 12203 Aug 6 01:02 app1.sql
-rw-rw-r-- 1 user user 12343 Aug 6 01:02 app2.sql
-rw-rw-r-- 1 user user 12238 Aug 6 01:02 app3.sql

I need to Trim the unwanted strings and my output should look like this:

app1.sql
app2.sql
app3.sql

I am not good at string functions...let me know if anyone can help me
out!!

cheers
--
Posted via http://www.ruby-forum.com/\.

Shekar Ls wrote:

Hi guys,
         I have the following information in a text file, which looks
like this:

-rw-rw-r-- 1 user user 12203 Aug 6 01:02 app1.sql
-rw-rw-r-- 1 user user 12343 Aug 6 01:02 app2.sql
-rw-rw-r-- 1 user user 12238 Aug 6 01:02 app3.sql

I need to Trim the unwanted strings and my output should look like this:

app1.sql
app2.sql
app3.sql

I am not good at string functions...let me know if anyone can help me
out!!

cheers

line = "-rw-rw-r-- 1 user user 12203 Aug 6 01:02 app1.sql"

pieces = line.split()
puts pieces[-1]

--output:--
app1.sql

···

--
Posted via http://www.ruby-forum.com/\.

awk "{print $NF}" my_file

···

On Aug 6, 1:31 am, Shekar Ls <idealo...@hotmail.com> wrote:

Hi guys,
         I have the following information in a text file, which looks
like this:

-rw-rw-r-- 1 user user 12203 Aug 6 01:02 app1.sql
-rw-rw-r-- 1 user user 12343 Aug 6 01:02 app2.sql
-rw-rw-r-- 1 user user 12238 Aug 6 01:02 app3.sql

I need to Trim the unwanted strings and my output should look like this:

app1.sql
app2.sql
app3.sql

I am not good at string functions...let me know if anyone can help me
out!!

cheers
--
Posted viahttp://www.ruby-forum.com/.

Hi,

···

Am Donnerstag, 06. Aug 2009, 15:31:51 +0900 schrieb Shekar Ls:

I have the following information in a text file, which looks
like this:

-rw-rw-r-- 1 user user 12203 Aug 6 01:02 app1.sql
-rw-rw-r-- 1 user user 12343 Aug 6 01:02 app2.sql
-rw-rw-r-- 1 user user 12238 Aug 6 01:02 app3.sql

I need to Trim the unwanted strings and my output should look like this:

app1.sql
app2.sql
app3.sql

As the file size will vary in width and the file names could
contain spaces this will be a little bit safer:

  l = "-rw-rw-r-- 1 user user 12238 Aug 6 01:02 app3.sql"
  filename = (l.split nil, 6).last[ 13..-1]

Bertram

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

If you have the whole contents of the file in a variable, then:

text = <<END
-rw-rw-r-- 1 user user 12203 Aug 6 01:02 app1.sql
-rw-rw-r-- 1 user user 12343 Aug 6 01:02 app2.sql
-rw-rw-r-- 1 user user 12238 Aug 6 01:02 app3.sql
END
    
files = text.scan(/\S+$/)
puts files

···

At 2009-08-06 02:31AM, "Shekar Ls" wrote:

          I have the following information in a text file, which looks
like this:

-rw-rw-r-- 1 user user 12203 Aug 6 01:02 app1.sql
-rw-rw-r-- 1 user user 12343 Aug 6 01:02 app2.sql
-rw-rw-r-- 1 user user 12238 Aug 6 01:02 app3.sql

I need to Trim the unwanted strings and my output should look like this:

app1.sql
app2.sql
app3.sql

--
Glenn Jackman
    Write a wise saying and your name will live forever. -- Anonymous

line = "-rw-rw-r-- 1 user user 12203 Aug 6 01:02 app1.sql"
pos = line.rindex(" ")
pos += 1
puts line[pos..-1]

--output:--
app1.sql

regex = / ([^ ]+)$/
md = regex.match(line)
puts md[1]

--output:--
app1.sql

···

--
Posted via http://www.ruby-forum.com/.

ruby -lane "puts $F[-1]" my_file

···

On Aug 6, 5:58 am, w_a_x_man <w_a_x_...@yahoo.com> wrote:

On Aug 6, 1:31 am, Shekar Ls <idealo...@hotmail.com> wrote:

> Hi guys,
> I have the following information in a text file, which looks
> like this:

> -rw-rw-r-- 1 user user 12203 Aug 6 01:02 app1.sql
> -rw-rw-r-- 1 user user 12343 Aug 6 01:02 app2.sql
> -rw-rw-r-- 1 user user 12238 Aug 6 01:02 app3.sql

> I need to Trim the unwanted strings and my output should look like this:

> app1.sql
> app2.sql
> app3.sql

> I am not good at string functions...let me know if anyone can help me
> out!!

> cheers
> --
> Posted viahttp://www.ruby-forum.com/.

awk "{print $NF}" my_file

Bertram Scharpf schrieb:
  > As the file size will vary in width and the file names could

contain spaces this will be a little bit safer:

  l = "-rw-rw-r-- 1 user user 12238 Aug 6 01:02 app3.sql"
  filename = (l.split nil, 6).last[ 13..-1]

Bertram

Why not just

   filename = (l.split nil, 9)[-1]

?

R.

Hi,

···

Am Donnerstag, 06. Aug 2009, 22:20:10 +0900 schrieb Rüdiger Bahns:

Bertram Scharpf schrieb:
> As the file size will vary in width and the file names could

contain spaces this will be a little bit safer:
  l = "-rw-rw-r-- 1 user user 12238 Aug 6 01:02 app3.sql"
  filename = (l.split nil, 6).last[ 13..-1]
Bertram

Why not just

  filename = (l.split nil, 9)[-1]

?

Because it is still allowed that filenames contain spaces (and
even other whitespaces what will fail with ls). Especially on
Samba mounts you have to expect that users heavily make use of it.

Bertram

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de