Trimming some string using ruby

S

Shekar Ls

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
 
S

Srijayanth Sridhar

[Note: parts of this message were removed to make it a legal post.]

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

Jayanth
 
7

7stud --

Shekar said:
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
 
7

7stud --

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
 
W

w_a_x_man

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

awk "{print $NF}" my_file
 
B

Bertram Scharpf

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
 
R

Rüdiger Bahns

Bertram said:
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.
 
B

Bertram Scharpf

Hi,

Am Donnerstag, 06. Aug 2009, 22:20:10 +0900 schrieb R=FCdiger Bahns:
Bertram said:
As the file size will vary in width and the file names could
contain spaces this will be a little bit safer:
l =3D "-rw-rw-r-- 1 user user 12238 Aug 6 01:02 app3.sql"
filename =3D (l.split nil, 6).last[ 13..-1]
Bertram

Why not just

filename =3D (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


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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top