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

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

Jayanth

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

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

Hi,

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

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

On Aug 6, 5:58 am, w_a_x_man [email protected] wrote:

-rw-rw-r-- 1 user user 12238 Aug 6 01:02 app3.sql
cheers

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

awk “{print $NF}” my_file

ruby -lane “puts $F[-1]” my_file

Bertram S. 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.

On Aug 6, 1:31 am, Shekar Ls [email protected] wrote:

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

Hi,

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

?

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

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

app2.sql
app3.sql

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