How to replace string in only one field which could be duplicated in the file

W

waterie

I have a file consists of serveral sections, and each section consists
of same/semilar fields. Some fields need to change, the string should
be replaced without effect same field in other section. Here is an
example,

group name = abc # section 1
stt = 123
busy = off
dev = name1
ssn = 129483021
dlc = c82u208
group name = xyz # section 2
stt = 145
busy = on
dev = name2
ssn = 129432326
dlc = s43k234
group name = jhg # section 3
stt = 321
busy = off
dev = name5
ssn = 198782383
dlc = d83j208

# And I need to replace some fields in each section, to have a new file
like

group name = abc # section 1
stt = 321
busy = on
dev = fieldx
ssn = 129483021
dlc = c82u208
group name = xxh # section 2
stt = 145
busy = on
dev = fieldy
ssn = 129432326
dlc = s43k234
group name = jhg # section 3
stt = 321
busy = off
dev = name5
ssn = 238764824
dlc = d83j143

Anybody has idea how to do this? I'm new to this kind of replacement,
really appreciate it!

Sally
 
A

Andrew McGregor

Anybody has idea how to do this? I'm new to this kind of replacement,
really appreciate it!

What have you tried? Very few will write the code for you. Some will
help you get _your_ code working.
 
A

Anno Siegel

I have a file consists of serveral sections, and each section consists
of same/semilar fields. Some fields need to change, the string should
be replaced without effect same field in other section. Here is an
example,

group name = abc # section 1
stt = 123
busy = off
dev = name1
ssn = 129483021
dlc = c82u208
group name = xyz # section 2
stt = 145
busy = on
dev = name2
ssn = 129432326
dlc = s43k234
group name = jhg # section 3
stt = 321
busy = off
dev = name5
ssn = 198782383
dlc = d83j208

# And I need to replace some fields in each section, to have a new file
like

group name = abc # section 1
stt = 321
busy = on
dev = fieldx
ssn = 129483021
dlc = c82u208
group name = xxh # section 2
stt = 145
busy = on
dev = fieldy
ssn = 129432326
dlc = s43k234
group name = jhg # section 3
stt = 321
busy = off
dev = name5
ssn = 238764824
dlc = d83j143

You expect your readers to compare 18 pairs of nonsensical lines to find
out where the changes are?
Anybody has idea how to do this? I'm new to this kind of replacement,
really appreciate it!

To do what? You haven't explained how to decide what changes to
make where. You will eventually have to explain that to the program.

In principle, you'd read each section into a hash whose keys and values
are the field names and values respectively. Then you inspect the hash
make any changes that apply (you have yet to specify that part). Then
write out the changed section to a new file.

It is unlikely that someone will write the code for you, even with
complete specifications. Show us what you've tried and we'll help
you get it right.

Anno
 
A

Anno Siegel

I have a file consists of serveral sections, and each section consists
of same/semilar fields. Some fields need to change, the string should
be replaced without effect same field in other section. Here is an
example,

group name = abc # section 1
stt = 123
busy = off
dev = name1
ssn = 129483021
dlc = c82u208
group name = xyz # section 2
stt = 145
busy = on
dev = name2
ssn = 129432326
dlc = s43k234
group name = jhg # section 3
stt = 321
busy = off
dev = name5
ssn = 198782383
dlc = d83j208

# And I need to replace some fields in each section, to have a new file
like

group name = abc # section 1
stt = 321
busy = on
dev = fieldx
ssn = 129483021
dlc = c82u208
group name = xxh # section 2
stt = 145
busy = on
dev = fieldy
ssn = 129432326
dlc = s43k234
group name = jhg # section 3
stt = 321
busy = off
dev = name5
ssn = 238764824
dlc = d83j143

You expect your readers to compare 18 pairs of nonsensical lines to find
out where the changes are?
Anybody has idea how to do this? I'm new to this kind of replacement,
really appreciate it!

To do what? You haven't explained how to decide what changes to
make where. You will eventually have to explain that to the program.

In principle, you'd read each section into a hash whose keys and values
are the field names and values respectively. Then you inspect the hash
and make any changes that apply (you have yet to specify that part).
Then write out the changed (or unchanged) section to a new file.

It is unlikely that someone will write the code for you, even with
complete specifications. Show us what you've tried and we'll help
you get it right.

Anno
 
U

usenet

I have a file consists of serveral sections, and each section consists
of same/semilar fields...

Is it absolutely necessary that your file be in this particular format?
Do you have control over the format, and could you make a small change
(to the manner in which you specify the group name) and organize the
file like as shown in the __DATA__ section of the script below???

By making this small change, the file looks like a Windows-style ".ini"
configuration file. Now you have the option to use powerful and easy
tools to manipulate it. I would investigate Config::IniFiles on CPAN.
With this module (or one of the many other ".ini" modules) your task
becomes much simpler. For example:

use strict; use warnings;
use Config::IniFiles;

my %cfg;
# I'm reading the __DATA__ section. Substitute a filename/handle, etc
tie %cfg, 'Config::IniFiles', ( -file => *main::DATA )
or die @Config::IniFiles::errors;

$cfg{'abc'}{'stt'} = 321; #unclear how this value is obtained
$cfg{'xyz'}{'dev'} = 'fieldy'; #also unclear
# ...etc... etc ... etc ...

tied( %cfg )->WriteConfig( '/tmp/new_config.ini' )
|| die "Could not write settings to new file.";

__DATA__
[abc]
stt = 123
busy = off
dev = name1
ssn = 129483021
dlc = c82u208
[xyz]
stt = 145
busy = on
dev = name2
ssn = 129432326
dlc = s43k234
 

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

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top