Advertisement

05.07.2008 at 02:56PM PDT, ID: 23384439 | Points: 125
[x]
Attachment Details

Replacing string while parsing file

I need to parse the file which is as below and store in a key value pair in a hasmap. While parsing i have to replace string which in @xx@, replace string will be from another hashmap whose key will also be same as @xx@ and value will be replacement string.
test_job: @sys_env@_test_od
test_box: @sys_env@_box_od
condition: s(@sys_env@_od)
std_out_file: @dep_logs@/test.out
std_err_file: @depy_logs@/test.err
profile: @dep_db_dir@/utilities/env.profile

for ex: @sys_env@ should be replaced and replacement value should be taken from %hash1 which contains @sys_env@ as key.

Attached code is parsing the file and storing key value, but i need help to replace string and store.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
open F,"<test.txt" or die "test.txt $!";
my %pforceContent;
while(<F>){
	chomp;
	my $content = $_;
	my @lines = split/\n/, $content;
	foreach (@lines) {
	    next unless /^(.+?):(.*)$/;
	    $pforceContent{$1}=$2;
	}		
}
close F;
Start your free trial to view this solution
Related Solutions: Parsing html content
Question Stats
Zone: Programming
Question Asked By: joshidin
Question Asked On: 05.07.2008
Participating Experts: 2
Points: 125
Views: 0
Translate:
Loading Advertisement...
05.07.2008 at 03:01PM PDT, ID: 21520723

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2008 at 03:07PM PDT, ID: 21520765

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2008 at 03:23PM PDT, ID: 21520873

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2008 at 03:34PM PDT, ID: 21520939

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2008 at 03:48PM PDT, ID: 21521024

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2008 at 04:05PM PDT, ID: 21521123

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2008 at 04:17PM PDT, ID: 21521192

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2008 at 07:49PM PDT, ID: 21521973

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2008 at 10:17PM PDT, ID: 21522472

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.08.2008 at 07:54AM PDT, ID: 21525268

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.08.2008 at 08:04AM PDT, ID: 21525355

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 02:59PM PDT, ID: 21536634

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • Automotive
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Displays / Monitors
  • Handhelds / PDAs
  • Components
  • Peripherals
  • Laptops/Notebooks
  • Servers
  • Misc
  • Apple
  • Embedded Hardware
  • Networking Hardware
  • Storage
  • Desktops
  • New Users
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMware
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Virtualization
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • Web Computing
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Consulting
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMware
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Automation
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Web Services
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Web Computing
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Lounge
  • Business Travel
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
  • Automotive
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
05.07.2008 at 03:01PM PDT, ID: 21520723

Rank: Genius

s/(\@\w*\@)/$hash1{$1}/
 
05.07.2008 at 03:07PM PDT, ID: 21520765
No...for example this line - test_job: @sys_env@_test_od
I have to consider @sys_env@  as a key while parsing and take a value from different hashmap with this key and finally store this line as a key value in a hashmap as below:
Key                       Value
test_job                  Replaced_test_od #(string looks like this after replacing @sys_env@);
 
05.07.2008 at 03:23PM PDT, ID: 21520873

Rank: Genius

$hash1{'@sys_env@'} = 'string looks like this after replacing @sys_env@';
$_ = 'test_job: @sys_env@_test_od';
$pforceContent{$2}=$_ if s/^((\w+).*?)(\@\w*\@)(.*)/$1$hash1{$3}$4/;
 
05.07.2008 at 03:34PM PDT, ID: 21520939
I'm sorry i do not understand your code properly...please give me full code snippet the way i have attached....
 
05.07.2008 at 03:48PM PDT, ID: 21521024

Rank: Genius

$hash1{'@sys_env@'} = 'string looks like this after replacing @sys_env@';
$hash1{'@dep_logs@'} = 'string looks like this after replacing @dep_logs@';
$hash1{'@depy_logs@'} = 'string looks like this after replacing @depy_logs@';
$hash1{'@dep_db_dir@'} = 'string looks like this after replacing @dep_db_dir@';
open F,"<test.txt" or die "test.txt $!";
my %pforceContent;
while(<F>){
        chomp;
         $pforceContent{$2}=$_ if s/^((.+?).*?)(\@\w*\@)(.*)/$1$hash1{$3}$4/;
}
close F;
 
05.07.2008 at 04:05PM PDT, ID: 21521123
Sorry..ur solution is not working.....String to parse is as below
test_job: @sys_env@_test_od
It should tokenize by ':' and store test_job as a key in $pforceContent and replaced string (replaced_test_od) as a value in $pforceContent.
You code is not taking care of considering string after ':'...it is taking full string.

 
05.07.2008 at 04:17PM PDT, ID: 21521192

Rank: Genius

$pforceContent{$2}=$_ if s/^((.+?).*?)(\@\w*\@)(.*)/$hash1{$3}$4/;
 
05.07.2008 at 07:49PM PDT, ID: 21521973
No...my requirement is simple, $perforceContent will have test_job as a key and Replaced_test_od as a value.
 
05.07.2008 at 10:17PM PDT, ID: 21522472

Rank: Genius

 $pforceContent{$1}= $hash1{$2} if /^(.*?):.*?\(@\w+@)/;
 
05.08.2008 at 07:54AM PDT, ID: 21525268
Getting this error after adding above piece of code.
 in regex; marked by <-- HERE in m/^(.*?):.*?\(@\w+@) <-- HERE / at C
c:\Test\parseHtml.pl line 125.
 
05.08.2008 at 08:04AM PDT, ID: 21525355
Attached is a file to parse and replace strings wich is in between @ symbol. Replace string will be stored in another hashmap as a value and key will be same as @xx@.

Please help me with the working code.
 
File to parse
 
 
05.09.2008 at 02:59PM PDT, ID: 21536634

Rank: Sage

I'm not sure I entirely understand what it is you're trying to do, but if I run the following script:

#!/usr/bin/perl -w
use strict;
my %pforceContent;
my %hash1 = (
    '@sys_env@' => 'Value for @sys_env@ key',
    '@user_logs@' => 'Value for @user_logs@ key',
    '@log_db_dir@' => 'Value for @log_db_dir@ key',
    '@dep_enter@' => 'Value for @dep_enter@ key',
    );

open (IN, "testread.txt") or die "Can't read testread.txt: $!\n";
while (<IN>) {
    next unless (/:/ && /\@/);
    while (/\@.+?\@/g) {
        my ($string, $key) = /(.+): .*?(\@.+?\@)/;
        $pforceContent{$string} = $hash1{$key};
    }
}

use Data::Dumper;
print Dumper (\%pforceContent);

__END__

against your example file, %pforceContent contains:

          'profile' => 'Value for @log_db_dir@ key',
          'std_out_file' => 'Value for @user_logs@ key',
          'box_name' => 'Value for @sys_env@ key',
          'insert_job' => 'Value for @sys_env@ key',
          'std_err_file' => 'Value for @user_logs@ key',
          'condition' => 'Value for @sys_env@ key',
          'command' => 'Value for @dep_enter@ key'


Is this close to what you're looking to do?
 
 
20080236-EE-VQP-29 / EE_QW_Related_20080208