Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Text file manipulation

by Sajn_00 (Initiate)
on Jan 17, 2020 at 03:26 UTC ( #11111508=perlquestion: print w/replies, xml ) Need Help??

Sajn_00 has asked for the wisdom of the Perl Monks concerning the following question:

Hi ,

I have two files, I have to update file 1, with the values from file2. for ex: in file1, ON_DIE_FILLER/filler_fuse should get value 0xa

I am having issues in comparing the names.

Please help.
file 1: ibl_ss_fuse_c0_r0/MRA_PVT_Tuning = 0x94, ibl_ss_fuse_c0_r0/S3M_SPARE_1B7_3to7 = 0x0, ON_DIE_FILLER/filler_fuse = 0x0, ON_DIE_FILLER/filler_byte = 0x0, file 2: die0_IAX_fuse_dino_c1_r0, die0_IAX_fuse_dino_c1_r0_IAX_fuse_iax_wr_ass +ist_pulsewidth, 0x5, die0_ON_DIE_FILLER, die0_ON_DIE_FILLER_ON_DIE_FILLER_filler_byte, 0xb, + die0_ON_DIE_FILLER, die0_ON_DIE_FILLER_ON_DIE_FILLER_filler_fuse, 0xa

Replies are listed 'Best First'.
Re: Text file manipulation
by GrandFather (Sage) on Jan 17, 2020 at 04:42 UTC

    Show us what you have tried where it's not working for you. It may help too if you tell us something about how big each file may be.

    Note that you are likely to get more help if you format you node so that it is easy to read. You can go back and update it now (but add an "updated" comment so people know the original content has changed).

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
Re: Text file manipulation
by BillKSmith (Prior) on Jan 18, 2020 at 18:14 UTC
    You need a function that compares two strings and returns "TRUE" if they are sufficiently similar and "FALSE" otherwise. In general, this is impossible. We must settle for a function that is good enough. We determine "good enough" by testing. Only you can determine how much testing is required. I doubt that your example of one match and several mismatches is sufficient. Here is a sample of test code using a match and a mismatch from your test cases. I have provided my first attempt (special_match) at you function to get you started.
    use strict; use warnings; use Test::More tests=>2; my $should_match = 1; my $should_not_match = 0; my @cases = ( ['ON_DIE_FILLER/filler_fuse', 'die0_ON_DIE_FILLER_ON_DIE_FILLER_filler_fuse', $should_match, ], ['ON_DIE_FILLER/filler_fuse', 'die0_ON_DIE_FILLER_ON_DIE_FILLER_filler_byte', $should_not_match, ], ); foreach my $case (@cases) { if ($case->[2] == $should_match) { ok( special_match($case->[0], $case->[1]), $case->[1]); } else { ok( !special_match($case->[0], $case->[1]), $case->[1]); } } sub special_match { my ($s1,$s2) = @_; $s1 =~ tr{/}{_}; # fix separator return $s2 =~ m/$s1$/; # $s1 at end of $s2 } OUTPUT: 1..2 ok 1 - die0_ON_DIE_FILLER_ON_DIE_FILLER_filler_fuse ok 2 - die0_ON_DIE_FILLER_ON_DIE_FILLER_filler_byte
    Bill
Re: Text file manipulation
by tybalt89 (Parson) on Jan 19, 2020 at 02:54 UTC
    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11111508 use warnings; open my $fh1, '<', \<<END; ibl_ss_fuse_c0_r0/MRA_PVT_Tuning = 0x94, ibl_ss_fuse_c0_r0/S3M_SPARE_1B7_3to7 = 0x0, ON_DIE_FILLER/filler_fuse = 0x0, ON_DIE_FILLER/filler_byte = 0x0, END open my $fh2, '<', \<<END; die0_IAX_fuse_dino_c1_r0, die0_IAX_fuse_dino_c1_r0_IAX_fuse_iax_wr_ass +ist_pulsewidth, 0x5, die0_ON_DIE_FILLER, die0_ON_DIE_FILLER_ON_DIE_FILLER_filler_byte, 0xb, die0_ON_DIE_FILLER, die0_ON_DIE_FILLER_ON_DIE_FILLER_filler_fuse, 0xa END my %values; while( <$fh2> ) { /([a-z][a-z_]*), (0x.),?$/ and $values{$1} = $2; } while( <$fh1> ) { s!([a-z][a-z_]*) = \K(\w+)(?=,$)! $values{$1} // $2 !e; print; }

    Outputs:

    ibl_ss_fuse_c0_r0/MRA_PVT_Tuning = 0x94, ibl_ss_fuse_c0_r0/S3M_SPARE_1B7_3to7 = 0x0, ON_DIE_FILLER/filler_fuse = 0xa, ON_DIE_FILLER/filler_byte = 0xb,

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11111508]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2020-02-27 18:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (121 votes). Check out past polls.

    Notices?