Advertisement

07.25.2003 at 10:37AM PDT, ID: 20690580
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Search & replace string in file
Tags: perl, replace, file, string, search
I want to pre-configure a software package that people download from a webserver. For this I'd like to preset some strings inside a binairy application file.

Basically what I need is a Perl procedure that will load the binairy file, look for a specific string, replace it with another one of the same length, and then save the file.

Doesn't sound too complicted, but I'm not a Perl expert, so that's why I am asking here :)

Thanks in advance,

P.
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: BorlandDelphi
Solution Provided By: holli
Participating Experts: 2
Solution Grade: A
Views: 527
Translate:
Loading Advertisement...
07.25.2003 at 11:28AM PDT, ID: 9006899

Rank: Master

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.

 
07.25.2003 at 11:37AM PDT, ID: 9006988

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.

 
07.25.2003 at 11:44AM PDT, ID: 9007056

Rank: Master

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.

 
07.25.2003 at 11:48AM PDT, ID: 9007080

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.

 
07.25.2003 at 11:49AM PDT, ID: 9007089

Rank: Master

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.

 
07.25.2003 at 11:49AM PDT, ID: 9007098

Rank: Master

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.

 
07.25.2003 at 12:16PM PDT, ID: 9007323

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.

 
07.25.2003 at 01:54PM PDT, ID: 9008106

Rank: Master

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.

 
07.25.2003 at 02:46PM PDT, ID: 9008410

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.

 
07.26.2003 at 11:04AM PDT, ID: 9011642

Rank: Master

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.

 
03.03.2004 at 07:03PM PST, ID: 10510762

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.

 
03.03.2004 at 11:37PM PST, ID: 10511900

Rank: Master

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
  • 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
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • 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
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • 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
  • 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
  • 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
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
07.25.2003 at 11:28AM PDT, ID: 9006899

Rank: Master

you can put the following into a batch-file:


@echo off
perl -0ni.old -e "s/^(.{offset})searchtext/\1replacement/;print;" %1
rem remove this to keep the original file
del %1.old
goto end

:error
echo Usage foo.bat [filename]
:end

change offset, searchtext and replacement to your needs.
 
07.25.2003 at 11:37AM PDT, ID: 9006988
What I would like is just a Perl script like Replace.pl with this function in it because it also has to read the replacement text from another file.

In other words what would a function in that script look like?
Also, what is the offset variable for, can you explain just a little? :)

Thanks,

P.
 
07.25.2003 at 11:44AM PDT, ID: 9007056

Rank: Master

just realized that binary files have some difficulties under windows, and that there is a small bug in the above code.
the following is better:

perl -0777 -ni.old -e "BEGIN { binmode (STDIN);binmode (STDOUT) }" -e "s/^(.{68})DEU/\1deu/;print;" command.com

(07.. is zero-seven, not o-seven)
 
07.25.2003 at 11:48AM PDT, ID: 9007080
It's a Unix web server, so I don't think I'll have any Windoze problems :)
 
07.25.2003 at 11:49AM PDT, ID: 9007089

Rank: Master

>>What I would like is just a Perl script like Replace.pl with this function in it because it also has to read the replacement text from another file.
fine.

In other words what would a function in that script look like?
Also, what is the offset variable for, can you explain just a little? :)
the offset is the position of the searchtext (here starting from the begining of the file), simply because binaries can contain anything.
if you cannot tell the offset, we have to find another regex that ensures that only the wanted part is replaced.
 
07.25.2003 at 11:49AM PDT, ID: 9007098

Rank: Master

just an assumption because of your nickname :-)
 
07.25.2003 at 12:16PM PDT, ID: 9007323
Delphi is for PC programming... I like my servers to be Unix though.

I'm still confused though....

This is what I would like...

$infile = '/datadir/infile.bin';
$outfile = '/datadir/outfile.bin';
$orgtext = 'hello world';
$newtext = 'HELLO WORLD';

sub create_file {
   load the file
   change the text
   save the file
}

I don not understand the -0ni.old -e "s/^(.{offset})searchtext/\1replacement/;print;" %1 line else I could put it in the function... can you explain it to me or show me how to convert it into the function? Perl for dummies is just not cutting it :)

Thanks!
 
07.25.2003 at 01:54PM PDT, ID: 9008106

Rank: Master

well, then.

$infile = '/datadir/infile.bin';
$outfile = '/datadir/outfile.bin';
$orgtext = 'hello world';
$newtext = 'HELLO WORLD';

undef $/; #slurp mode, read files in a whole

open IN, $infile or die $!;
$intext = <IN>;
close IN;

$intext = s/$orgtext/$newtext/ms;

open OUT, ">$outfile" or die $!;
print OUT $intext;
close OUT;
Accepted Solution
 
07.25.2003 at 02:46PM PDT, ID: 9008410
Cool... after going through my Perl for dummies book I came up with

$count = ($data =~ s/$orgtext/$newtext/g);

I'm sure yours is better though :)

Thanks for all your help!
 
07.26.2003 at 11:04AM PDT, ID: 9011642

Rank: Master

not better. different.
the was a typo in mine, sorry. it should have been:

$intext =~ s/$orgtext/$newtext/ms;

the ms is for coping correctly with newlines (that can easily appear in a binary).

$count = ($data =~ s/$orgtext/$newtext/g);

replaces ALL occurrences of orgtext with newtext and places the number of occurences in $count.



 
03.03.2004 at 07:03PM PST, ID: 10510762
Hello
The code above also worked for me but having problem with the search string if has (NBL) for example if $orgtext = 'hello world (NBL)';
What I need to do ?

 
03.03.2004 at 11:37PM PST, ID: 10511900

Rank: Master

this is because the brackets () are special chars in the regexp. you need to escape them.

$orgtext = 'hello world \(NBL\)';
 
 
20080236-EE-VQP-29