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) .
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
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) .
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
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).
On Wed, Jul 27, 2011 at 7:54 AM, kotin 76 <kotin76@yahoo.com> wrote:
Hi Hans Mackowiak 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
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 Cheek wrote in post #1013337:
···
On Wed, Jul 27, 2011 at 7:54 AM, kotin 76 <kotin76@yahoo.com> 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).
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 Cheek wrote in post #1013337:
> On Wed, Jul 27, 2011 at 7:54 AM, kotin 76 <kotin76@yahoo.com> 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%/"
> # >>
i did not get the how i get logic for my requirement from rubular.
regards
kotin
Josh Cheek wrote in post #1013351:
···
On Wed, Jul 27, 2011 at 8:26 AM, kotin 76 <kotin76@yahoo.com> 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"
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.
···
On Wed, Jul 27, 2011 at 8:58 AM, kotin 76 <kotin76@yahoo.com> wrote:
Hi josh,
i did not get the how i get logic for my requirement from rubular.