String manuplation example

Hi,

I am new to ruby.

I tried to extract the word from /dev/sda4 from below string. I did not
get exact logic for this.

the string is below one( spaces are there between words) .

string = “/dev/sda4 45225016 3702464 39225208 9% /”

please any one let me know the logic to extract word from /dev to endof
that word. it mean up to /dev/sda4.

i would appreciate any answer.

Regards
Kotin

string.match(/[/\w]+/)[0]#=> “/dev/sda4”

You have static value ‘/dev/sda4’? then you can do the following
You can do gsub
i.e st = “/dev/sda4 45225016 3702464 39225208 9% /”
stv = st.gsub("/dev/sda4")
your_value = stv.to_a[0]

kotin 76 wrote in post #1013327:

Hi,

I am new to ruby.

I tried to extract the word from /dev/sda4 from below string. I did not
get exact logic for this.

the string is below one( spaces are there between words) .

string = “/dev/sda4 45225016 3702464 39225208 9% /”

please any one let me know the logic to extract word from /dev to endof
that word. it mean up to /dev/sda4.

i would appreciate any answer.

Regards
Kotin

Hi Hans M. and Shah,

Thanks for your quick reply.

I am executed your code. its giving string /dev/sda4.

its really use full for me.

just i want to know onemore logic for

  1. i dont know how many character will come after /dev like it may come
    /dev/sda4 , /dev/sda1256 or /dev/sdaERT7890. But my aim is to extract
    the that word from /dev to end of that word like “/dev/sda4” ,
    “/dev/sda1256” , “/dev/sdaERT7890”. for this what logic i need to add
    after /dev in below of your logics

    stv = st.gsub("/dev/?")
    string.match(/[/\w]+/)[0]#=> “/dev/?”

Random string like

string = “/dev/sda4 45225016 3702464 39225208 9% /”
string = “/dev/sda1256 45225016 3702464 39225208 9% /”
string = “/dev/sdaERT7890 45225016 3702464 39225208 9%
/”
string = “/dev/sda4sdfsd 45225016 3702464 39225208 9%
/”
string = “/dev/sda4fsdfs 45225016 3702464 39225208 9%
/”

form above string i need to extract /dev/sda4 , /dev/sda1256,
/dev/sdaERT7890 , /dev/sda4sdfsd and /dev/sda4fsdfs

please suggest your logic

Regards
Kotin

Chirag Shah wrote in post #1013329:

You have static value ‘/dev/sda4’? then you can do the following
You can do gsub
i.e st = “/dev/sda4 45225016 3702464 39225208 9% /”
stv = st.gsub("/dev/sda4")
your_value = stv.to_a[0]

kotin 76 wrote in post #1013327:

Hi,

I am new to ruby.

I tried to extract the word from /dev/sda4 from below string. I did not
get exact logic for this.

the string is below one( spaces are there between words) .

string = “/dev/sda4 45225016 3702464 39225208 9% /”

please any one let me know the logic to extract word from /dev to endof
that word. it mean up to /dev/sda4.

i would appreciate any answer.

Regards
Kotin

Then you can go with only regular expression given by Hans
i.e
st = “/dev/sda1256 45225016 3702464 39225208 9% /”
st.match(/[/\w]+/)[0]
this will give you your search

if you use match(/[/\w]+/)[0]
you will get all chars and / up to the first non alpha numeric, (like an
space ) so it does work with all your strings

On Wed, Jul 27, 2011 at 7:54 AM, kotin 76 [email protected] wrote:

  1. i dont know how many character will come after /dev like it may come
    Random string like

It’s not really clear to me what “extract” means. Are you interested in
knowing what it begins with, or are you interested in removing it from
the
string, or both?

Anyway, looking at your examples, this works. But if you end up with
spaces
in the filenames, it won’t work (hence the reason I think it’s stupid
how
Unix loves turning everything into whitespace separated values).

string = “/dev/sda4 45225016 3702464 39225208 9% /
/dev/sda1256 45225016 3702464 39225208 9% /
/dev/sdaERT7890 45225016 3702464 39225208 9%/
/dev/sda4sdfsd 45225016 3702464 39225208 9%/
/dev/sda4fsdfs 45225016 3702464 39225208 9%/”

string.each_line do |line|
first, rest = line.split(/\s+/, 2)
p first
p rest
puts
end

>> “/dev/sda4”

>> “45225016 3702464 39225208 9% /\n”

>>

>> “/dev/sda1256”

>> “45225016 3702464 39225208 9% /\n”

>>

>> “/dev/sdaERT7890”

>> “45225016 3702464 39225208 9%/\n”

>>

>> “/dev/sda4sdfsd”

>> “45225016 3702464 39225208 9%/\n”

>>

>> “/dev/sda4fsdfs”

>> “45225016 3702464 39225208 9%/”

>>

Hi All,

Thanks for your quick reply’s.

I executed the program from wt hans suggestd. it works fine.

some times i am going to extract at other location like below

string = "45225016 3702464 39225208 9% /dev/sda4 "
string = "45225016 3702464 39225208 9% /dev/sda1256 "
string = "45225016 3702464 39225208 9% /dev/sdaERT7890 "
string = "45225016 3702464 39225208 9 /dev/sda4sdfsd "
string = "45225016 3702464 39225208 9% /dev/sda4fsdfs "

i understand one thing,

we can success if we create the logic to extract string starting with
/dev and to up to the first non alpha numeric.

have you suggest any idea for this?

Regards
kotin

Josh C. wrote in post #1013337:

On Wed, Jul 27, 2011 at 7:54 AM, kotin 76 [email protected] wrote:

  1. i dont know how many character will come after /dev like it may come
    Random string like

It’s not really clear to me what “extract” means. Are you interested in
knowing what it begins with, or are you interested in removing it from
the
string, or both?

Anyway, looking at your examples, this works. But if you end up with
spaces
in the filenames, it won’t work (hence the reason I think it’s stupid
how
Unix loves turning everything into whitespace separated values).

string = “/dev/sda4 45225016 3702464 39225208 9% /
/dev/sda1256 45225016 3702464 39225208 9% /
/dev/sdaERT7890 45225016 3702464 39225208 9%/
/dev/sda4sdfsd 45225016 3702464 39225208 9%/
/dev/sda4fsdfs 45225016 3702464 39225208 9%/”

string.each_line do |line|
first, rest = line.split(/\s+/, 2)
p first
p rest
puts
end

>> “/dev/sda4”

>> “45225016 3702464 39225208 9% /\n”

>>

>> “/dev/sda1256”

>> “45225016 3702464 39225208 9% /\n”

>>

>> “/dev/sdaERT7890”

>> “45225016 3702464 39225208 9%/\n”

>>

>> “/dev/sda4sdfsd”

>> “45225016 3702464 39225208 9%/\n”

>>

>> “/dev/sda4fsdfs”

>> “45225016 3702464 39225208 9%/”

>>

On Wed, Jul 27, 2011 at 8:26 AM, kotin 76 [email protected] wrote:

string = "45225016 3702464 39225208 9% /dev/sdaERT7890 "
Regards

  1. i dont know how many character will come after /dev like it may come
    in the filenames, it won’t work (hence the reason I think it’s stupid
    string.each_line do |line|

>> “45225016 3702464 39225208 9% /\n”


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

Here, I put your strings into rubular Rubular: dev

How about trying to create a regex that matches your data and then
posting
it here for feedback.

Hi josh,

i did not get the how i get logic for my requirement from rubular.

regards
kotin

Josh C. wrote in post #1013351:

On Wed, Jul 27, 2011 at 8:26 AM, kotin 76 [email protected] wrote:

string = "45225016 3702464 39225208 9% /dev/sdaERT7890 "
Regards

  1. i dont know how many character will come after /dev like it may come
    in the filenames, it won’t work (hence the reason I think it’s stupid
    string.each_line do |line|

>> “45225016 3702464 39225208 9% /\n”


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

Here, I put your strings into rubular Rubular: dev

How about trying to create a regex that matches your data and then
posting
it here for feedback.

hm use //dev/[\w]+/ then the position is not the problem

On Wed, Jul 27, 2011 at 8:58 AM, kotin 76 [email protected] wrote:

Hi josh,

i did not get the how i get logic for my requirement from rubular.

regards
kotin

You will need to write a regular expression to match the data in your
string. The regex goes into the top where I’ve typed “dev” the string is
in
the bottom left, and wherever the regex matches the string is shown in
the
bottom right.

The rules for writing regexes (is that the plural or is it “regexen”?)
are
listed below that in the quick reference. As you experiment, by changing
the
regex in the top bar, pay attention to how the highlighting in the lower
right box changes.

Hi All thanks for your quick replys. I got solution.

On Jul 27, 2011, at 10:20 AM, Hans M. wrote:

hm use //dev/[\w]+/ then the position is not the problem

And if you don’t want to keep escaping the /'s because you’re matching
pathnames:

%r{/dev/\w+}

is the same using the %r{ } syntax for writing a literal Regexp.

-Rob

Rob B.
[email protected] http://AgileConsultingLLC.com/
[email protected] http://GaslightSoftware.com/

On Wed, Jul 27, 2011 at 5:33 PM, kotin 76 [email protected] wrote:

Hi All thanks for your quick replys. I got solution.

I think nobody suggested String#[] so far. You can do

device = str[%r{/dev/sd\S+}]

Kind regards

robert