CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
string overwrite / replace

 



dkhanna01
Novice

Jul 7, 2009, 1:15 PM

Post #1 of 7 (1082 views)
string overwrite / replace Can't Post

I have a file that looks like this:

fv09 fv09-dev 5 y GRPLOAD V NONE 14 # fv09-Release
jul0809 jul0809-dev 5 y GRPLOAD V NONE 14 # jul0809-Release
jul2209 jul2209-dev 5 y GRPLOAD V NONE 14 # jul2209-Release


Now I need to replace string "NONE" with another string like "09/01/01:01:01". Now I can easily make this happened by doing :

$tmp = "09/01/01:01:01";

$str =~ s/NONE/$tmp/ ;

Now by doing this this will give me an output like :

fv09 fv09-dev 5 y GRPLOAD V NONE 14 # fv09-Release
jul0809 jul0809-dev 5 y GRPLOAD V 09/01/01:01:01 14 # jul0809-Release
jul2209 jul2209-dev 5 y GRPLOAD V NONE 14 # jul2209-Release


Which is more whie spaces after the value when I replace NONE (Assuming that I'm only changing the second line not all the lines)

But I want something like

fv09 fv09-dev 5 y GRPLOAD V NONE 14 # fv09-Release
jul0809 jul0809-dev 5 y GRPLOAD V 09/01/01:01:01 14 # jul0809-Release
jul2209 jul2209-dev 5 y GRPLOAD V NONE 14 # jul2209-Release


For doing this if I write command like :

$str =~ s/NONE /$tmp/ ;

THat didn't work because in my file I don't know whether I have tab/spaces etc after string "NONE"


johnpc831
Novice

Jul 7, 2009, 2:17 PM

Post #2 of 7 (1075 views)
Re: [dkhanna01] string overwrite / replace [In reply to] Can't Post

It's hard for me to visually see the difference between what you need and what you want, but if you want to replace the word "NONE" and all the white space that follows, you could use:


Code
$str =~ s/NONE\s+/$tmp/;



dkhanna01
Novice

Jul 7, 2009, 2:21 PM

Post #3 of 7 (1073 views)
Re: [johnpc831] string overwrite / replace [In reply to] Can't Post

Basically I need to indent and replace the string :

"NONE " with

"09/01/01-01:01 " and not

"09/01/01-01:01 "


1arryb
User

Jul 7, 2009, 2:22 PM

Post #4 of 7 (1073 views)
Re: [dkhanna01] string overwrite / replace [In reply to] Can't Post

Hi dkhanna,

You write:

Quote
Now by doing this this will give me an output like :

fv09 fv09-dev 5 y GRPLOAD V NONE 14 # fv09-Release
jul0809 jul0809-dev 5 y GRPLOAD V 09/01/01:01:01 14 # jul0809-Release
jul2209 jul2209-dev 5 y GRPLOAD V NONE 14 # jul2209-Release


Which is more whie spaces after the value when I replace NONE (Assuming that I'm only changing the second line not all the lines)

But I want something like

fv09 fv09-dev 5 y GRPLOAD V NONE 14 # fv09-Release
jul0809 jul0809-dev 5 y GRPLOAD V 09/01/01:01:01 14 # jul0809-Release
jul2209 jul2209-dev 5 y GRPLOAD V NONE 14 # jul2209-Release


I diff'ed the two blocks and they are identical. Please try again?

Cheers,

Larry


johnpc831
Novice

Jul 7, 2009, 2:32 PM

Post #5 of 7 (1070 views)
Re: [dkhanna01] string overwrite / replace [In reply to] Can't Post


Quote
Basically I need to indent and replace the string :

"NONE " with

"09/01/01-01:01 " and not

"09/01/01-01:01 "


Oh, well in that case you should be able to use:


Code
$str =~ s/NONE\s+/$tmp /;



dkhanna01
Novice

Jul 7, 2009, 2:46 PM

Post #6 of 7 (1067 views)
Re: [johnpc831] string overwrite / replace [In reply to] Can't Post

I'm not sure but I think this forums while posting removes any trailing white spaces.. but any whay I firgure out the problem thanks everyone for your quick response


KevinR
Veteran


Jul 7, 2009, 2:50 PM

Post #7 of 7 (1066 views)
Re: [dkhanna01] string overwrite / replace [In reply to] Can't Post


In Reply To
I'm not sure but I think this forums while posting removes any trailing white spaces.. but any whay I firgure out the problem thanks everyone for your quick response


use the code tags.
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives