Binary patch using perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Binary patch using perl
# 1  
Old 12-15-2017
Binary patch using perl

for all who are interested:

  • a bash function scanning all unique strings in FILE that contain a token and replacing it.
  • Furthermore the function demonstrates how to construct a perl command from variables in a script:

Code:
 patchfile() {
        local -r file="$1"
        local -r tok="$2"
        local -r replaceby="$3"
        local rc=0
...
        # Find all unique strings in FILE that contain the pattern
        perl -pi -wE 's/'$tok'/'$replaceby'/g' $file
        rc=$?
...
        return $rc
    }

# 2  
Old 12-15-2017
Quote:
Originally Posted by dodona
for all who are interested:

  • a bash function scanning all unique strings in FILE that contain a token and replacing it.
  • Furthermore the function demonstrates how to construct a perl command from variables in a script:

Code:
 patchfile() {
        local -r file="$1"
        local -r tok="$2"
        local -r replaceby="$3"
        local rc=0
...
        # Find all unique strings in FILE that contain the pattern
        perl -pi -wE 's/'$tok'/'$replaceby'/g' $file
        rc=$?
...
        return $rc
    }

Note that with the quoting you're using on the perl command you're constructing, you won't get the desired results if any of $file, $tok, and $replaceby expand to strings that contain any <space>, <tab>, or <newline> characters. A safer and shorter way to write that line would be:
Code:
        perl -pi -wE "s/$tok/$replaceby/g" "$file"

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert binary file to csv and then back to the binary format

Hello *nix specialists, Im working for a non profit organisation in Germany to transport DSL over WLAN to people in areas without no DSL. We are using Linksys WRT 54 router with DD-WRT firmware There are at the moment over 180 router running but we have to change some settings next time. So my... (7 Replies)
Discussion started by: digidax
7 Replies

2. Shell Programming and Scripting

Displaying a number in binary using perl

printf FH2" 3'b%b : begin\n",$i; where i is an integer in the loop is displaying 3'b1 : begin expected output was 3'b001 : begin (1 Reply)
Discussion started by: dll_fpga
1 Replies

3. Shell Programming and Scripting

Convert binary to text Perl script

Hello everyone, I have a binary file with a structure unknown. I have found 2 perl scripts that it seems to do the convertion but I get sintactic errors when I run them, may somebody test these 2 scripts please and see if really work? One if from here... (10 Replies)
Discussion started by: Ophiuchus
10 Replies

4. Shell Programming and Scripting

Perl : find perl executable binary

Hi I have an oracle perl script running as cron job across multiple unix servers. The issue is the perl binary is found in multiple directories I use in the start of the script ... #!/usr/bin/perl on some servers the script fails because /usr/bin/perl is not present. Is there a way i can... (4 Replies)
Discussion started by: PrasannaKS
4 Replies

5. Shell Programming and Scripting

capturing C++ binary return status in perl

Hello, I have a C++ binary that runs in my perl script. But, Currently, the binary is doing a core dump and i want to capture the retrun status of the binary to report as an issue. Can you please help me on this. Thanks, Sateesh (1 Reply)
Discussion started by: kotasateesh
1 Replies

6. Shell Programming and Scripting

capturing C++ binary return status in perl

Hello, I have a C++ binary that runs in my perl script. But, Currently, the binary is doing a core dump and i want to capture the retrun status of the binary to report as an issue. Can you please help me on this. Thanks, Sateesh (1 Reply)
Discussion started by: kotasateesh
1 Replies

7. Solaris

compiled binary file gives "cannot execute binary file"

Hi, I have two Solaris machines. 1. SunOS X 5.8 Generic_108528-29 sun4u sparc SUNW,Sun-Blade-1500 2. SunOS Y 5.8 Generic_108528-13 sun4u sparc SUNW,Ultra-60 I am trying to buiild a project on both these machines. The Binary output file compiled on machine 2 runs on both the machines. Where... (0 Replies)
Discussion started by: scgupta
0 Replies

8. IP Networking

Patch-o-matic (patch for iptable) for linux2.4.08 & iptable1.2.7a

Hello friends I'm running Redhat 9.0 with linux kernel 2.4.20-8 & have iptables version 1.2.7a & encountering a problem that I narrate down. I need to apply patch to my iptable and netfilter for connection tracking and load balancing that are available in patch-o-matic distribution by netfilter.... (0 Replies)
Discussion started by: Rakesh Ranjan
0 Replies

9. Shell Programming and Scripting

Perl and binary workings

Perhaps it's me - maybe I'm dumb and am getting this working solution wrong... I have a binary value 00000011 in $binVal and I want to print the result in denary, so in perl I did as the perldoc -f oct told me to do and added a 0b prefix as so: $binVal = "0b" . $binVal; then I wanted to... (5 Replies)
Discussion started by: WIntellect
5 Replies
Login or Register to Ask a Question