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

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on How to Generate text file by picking up string from other text file line by line

Replies are listed 'Best First'.
Re: How to Generate text file by picking up string from other text file line by line
by GrandFather (Saint) on Dec 09, 2016 at 10:53 UTC

    I can't see the code you have tried and you don't tell us how it fails. Also, is your data really one big block like that, or were you too lazy to format your node nicely even though it looked awful when you previewed it?

    Premature optimization is the root of all job security
Re: How to Generate text file by picking up string from other text file line by line
by talexb (Chancellor) on Dec 09, 2016 at 15:45 UTC

    Welcome to the Monastery!

    Here's how you should have formatted your post:

      My first file data.txt from where I want to pick string is

      ip mac 172.16.1.1 00-17-7c-0b-d4-ac 172.16.1.39 c4-17-fe-07-94-33 172.16.1.59 44-37-e6-ed-34-92 172.16.1.61 00-e0-4a-0a-2a-0a 172.16.1.63 00-30-18-a8-8b-ba 172.16.1.65 90-2b-34-5f-44-38 172.16.1.70 44-37-e6-ed-ab-a3

      and I want to generate file aclallow.conf in format as below,

      http_access allow mac1 ip1 http_access allow mac2 ip2 http_access allow mac3 ip3 http_access allow mac4 ip4 http_access allow mac5 ip5

      and aclip.conf as below

      acl ip1 src 172.16.1.55 acl ip2 src 172.16.1.56 acl ip3 src 172.16.1.57 acl ip4 src 172.16.1.58 acl ip5 src 172.16.1.59

      and aclmac.conf as below

      acl mac1 arp 00:e0:4c:25:fc:9b acl mac2 arp 00:1F:D0:E0:73:F5 acl mac3 arp 00:25:64:AC:31:17 acl mac4 arp 44:37:e6:ed:d0:ae acl mac5 arp 44:37:e6:ed:34:92

      Please help me for this .. Thanks in advance ..

    I have taken the liberty of capitalizing the first words in a sentence, and capitalizing 'I', as is common when writing English.

    The purpose of this community is to educate folks who are interested in Perl. As such, we are glad to help anyone who has posted some code -- but with which they're having a problem.

    We're not going to solve the problem for you -- you need to make solid effort first, showing us how you solved the problem, and where you got stuck.

    Good luck!

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Re: How to Generate text file by picking up string from other text file line by line
by NetWallah (Canon) on Dec 09, 2016 at 16:07 UTC
    Try this code. - you may need to adjust the template when specifications are clearer:
    #!/usr/bin/perl use strict; use warnings; my %output=( 'aclallow.conf' => {FH=>undef, TEMPLATE=>sub{my ($ip,$mac) = @_; +return "http_access allow $mac $ip"}}, 'aclip.conf' => {FH=>undef, TEMPLATE=>sub{my ($ip,$mac) = @_; +return "acl ip1 src $ip"}}, 'aclmac.conf' => {FH=>undef, TEMPLATE=>sub{my ($ip,$mac) = @_; +return "acl mac1 arp $mac"}}, ); my $inputfile="data-ip.txt"; open my $fh,"<", $inputfile or die "ERROR: Cannot open '$inputfile' : +$!"; for (keys %output){ open ($output{$_}{FH}, ">", $_) or die "Error: Could not open $_ f +or output:$!"; } while (<$fh>){ chomp; my ($ip,$mac) = split; next unless $ip and $ip=~/^\d/; for (keys %output){ print {$output{$_}{FH}} $output{$_}{TEMPLATE}->($ip,$mac), "\n" +; } } close $fh; for (keys %output){ close ($output{$_}{FH}); }

            ...it is unhealthy to remain near things that are in the process of blowing up.     man page for WARP, by Larry Wall