Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Read and Create File

by szpt9m (Novice)
on Jun 28, 2021 at 06:39 UTC ( [id://11134376]=perlquestion: print w/replies, xml ) Need Help??

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

Hello experts, I am new to Perl and need to write a perl script to read a file and find some string and generate another file based on that. Here's what i am looking for now. My Input is a file containing
&LOG &LOG TEXT: "C:\temp\xyz.txt" &LOG Action: DEFAULT &LOG Naming_Technique: DEFAULT_NAMING Default_Name: @ST/ABC1234/00001 &LOG Container: "current:entity" &LOG Type: SIMILAR &LOG Name: ABC-Y &LOG Description: ABC-Z &LOG Associated_Files_Directory: "" &LOG &LOG TEXT: "C:\temp\abc.txt" &LOG Action: DEFAULT &LOG Naming_Technique: DEFAULT_NAMING Default_Name: @ST/ABC5678/00001 &LOG Container: "current:entity" &LOG Type: SIMILAR &LOG Name: EWQ-Y &LOG Description: EWQ-Z &LOG Associated_Files_Directory: "" &LOG &LOG TEXT: "C:\temp\rtq.txt" &LOG Action: DEFAULT &LOG Naming_Technique: DEFAULT_NAMING Default_Name: @ST/ABC2345/00002 &LOG Container: "current:entity" &LOG Type: SIMILAR &LOG Name: ERD-Y &LOG Description: ERD-Z &LOG Associated_Files_Directory: "" &LOG
My script has to read the above file and get the lines with TEXT: and Naming_Technique: and create another file as below
[xyz.txt] db_part_no=ABC1234 db_part_rev=00001 [abc.txt] db_part_no=ABC5678 db_part_rev=00001 [rtq.txt] db_part_no=ABC2345 db_part_rev=00002
Can someone help me here please?

Replies are listed 'Best First'.
Re: Read and Create File
by choroba (Cardinal) on Jun 28, 2021 at 07:01 UTC
    Read the file line by line. Use regexes to extract parts of text. Use /x to make them readable and commentable.

    Also, I used the variable $printed to print a newline before each section except the first one.

    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; my $printed; while (<>) { chomp; if (/^&LOG[ ]TEXT:[ ] " # The opening quote. [^"]*\\ # Path up to the last backslash. ( [^\\"]+ ) # The final part of the path. " # The closing quote. $/x ) { print "\n" if $printed++; say "[$1]"; } elsif (m{^&LOG[ ]Naming_Technique:[ ] [^@]+\@ # Anything up to the @. \w+/ # Word characters followed by a slash. ([^/]+)/ # The part_no. ([^/]+) # the part_rev. $}x ) { say "db_part_no=$1"; say "db_part_rev=$2"; } }

    Save as create-ini.pl, run as

    perl create-ini.pl input-file > output-file.ini

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      Really nice. Working as expected. Thanks a lot for quick help
Re: Read and Create File
by BillKSmith (Monsignor) on Jun 28, 2021 at 13:55 UTC
    Process the file by logical record. Take advantage of the fixed format.
    use strict; use warnings; while ( $_ = do { local $/ = qq(""\n); <> }) { if (/(\w+\.\w+).*Default_Name:\s\@ST\/(\w+)\/(\w+)$/ms) { print "[$1]\n", "db_part_no=$2\n", "db_part_rev=$3\n\n"; } }

    Output with input file from OP

    >perl szpt9m.pl 11134376.txt [xyz.txt] db_part_no=ABC1234 db_part_rev=00001 [abc.txt] db_part_no=ABC5678 db_part_rev=00001 [rtq.txt] db_part_no=ABC2345 db_part_rev=00002
    Bill

      An excess of caution would suggest adding a defined test to the while-loop conditional. This test is not added by default by the compiler in the case of a complex conditional expression like the do-block:
          while ( defined($_ = do { ... }) ) { ... }


      Give a man a fish:  <%-{-{-{-<

        Yes, good practice does require the 'defined'. In this application, I am already assuming the format of a 'block'. The special case (block is defined, but evaluates to FALSE), which requires 'defined', should never happen.
        Bill
        Hello, Thanks a lot for your response and is working fine for the fixed format given before. Now I have a situation where I may get somehting like this
        &LOG &LOG TEXT: "C:\temp\xyz.txt" &LOG Action: DEFAULT &LOG Naming_Technique: DEFAULT_NAMING Default_Name: @ST/ABC1234/00001 &LOG Container: "current:entity" &LOG Type: SIMILAR &LOG Name: ABC-Y &LOG Description: ABC-Z &LOG Associated_Files_Directory: "" &LOG &LOG TEXT: "C:\temp\PQR.txt" &LOG Naming_Technique: USER_NAME Default_Name: @ST/ABC9999/00001 &LOG &LOG &LOG TEXT: "C:\temp\abc.txt" &LOG Action: DEFAULT &LOG Naming_Technique: DEFAULT_NAMING Default_Name: @ST/ABC5678/00001 &LOG Container: "current:entity" &LOG Type: SIMILAR &LOG Name: EWQ-Y &LOG Description: EWQ-Z &LOG Associated_Files_Directory: "" &LOG &LOG TEXT: "C:\temp\rtq.txt" &LOG Action: DEFAULT &LOG Naming_Technique: DEFAULT_NAMING Default_Name: @ST/ABC2345/00002 &LOG Container: "current:entity" &LOG Type: SIMILAR &LOG Name: ERD-Y &LOG Description: ERD-Z &LOG Associated_Files_Directory: "" &LOG
        Here I do not want PQR.txt and its number ABC9999 in the output. Difference here is that, whenever I have USERNAME after Naming_Technique, i should avoid its .txt and number in the output. So my output remains same as before
        [xyz.txt] db_part_no=ABC1234 db_part_rev=00001 [abc.txt] db_part_no=ABC5678 db_part_rev=00001 [rtq.txt] db_part_no=ABC2345 db_part_rev=00002
        could you please help?

      You are of course assuming that the Associated_Files_Directory field will always be equal to "" and that it will always be the last field in the record.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-16 20:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found