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

Hi! Trying to replace srings in file.Input Csv file contains 5 columns. First column contains name. If name ends with AS then it should be repalced with "1" ($line =~ s/(.*\sAS)/1/g;) when AS is not present then the value should be "0"!!!. 2 column- no need to change but has to be moved in first place and the first column needs to moved in 5th place with replaced strings. 3;4;5 colums I use substitute to change strings. If there is no value in 3;4;5 colums then I need to insert "0" (dont know how to do that) At the end of the line there should be added 1 more column - todays date "strftime(".%Y%m%d", localtime);" !!! Input file:
E. name;E.serial;E. kive;E. bilanss;E. emp 01 Norme OÜ;10818160;less 1 miljoni;less 1 miljoni; 1 AGENCY OÜ;10652268;less 1 miljoni;less 1 miljoni;1 - 5 emp 1. JALASTAAP;70007295;;; 101 SYSTEMS OÜ;10825652;3 - 10 miljonit;1 - 3 miljonit; 15075 OÜ;10399876;1 - 3 miljonit;less 1 miljoni;1 - 5 emp 5SVARA AS;11036261;;; 1QA OÜ;10614794;less 1 miljoni;less 1 miljoni;
output file:
10818160|1|1|0|0|2010-05-12| 10652268|1|1|5|0|2010-05-12| 70007295|0|0|0|0|2010-05-12| 10825652|10|3|0|0|2010-05-12| 10399876|3|1|5|0|2010-05-12| 11036261|0|0|0|1|2010-05-12| 10614794|1|1|0|0|2010-05-12|
#! C:\Perl64\bin\perl use strict; use POSIX qw(strftime); my $add_date = strftime(".%Y%m%d", localtime); my $output_tag = (".csv"); my $failid='C:\\ROOT_DIR\\Inspektor\\insp_file.aa'; my $dest = "C:\\ROOT_DIR\\Inspektor\\test\\"; if (open(FAILIDcnt, $failid)) { while(my $input = <FAILIDcnt>) { chomp($input); my $output = $input; open(IF, 'C:\\ROOT_DIR\\Inspektor\\' .$input) or next; open(my $dest,"> $dest".$output) or die "Can't open $dest: $! +"; while(my $line = <IF>) { $line =~ s/less 1 miljoni/1/g; $line =~ s/1 - 3 miljonit/3/g; $line =~ s/3 - 10 miljonit/10/g; $line =~ s/10 - 20 miljonit/20/g; $line =~ s/20 - 50 miljonit/30/g; $line =~ s/50 - 100 miljonit/100/g; $line =~ s/100 - 250 miljonit/102/g; $line =~ s/250 - 500 miljonit/103/g; $line =~ s/500 milj - 1 miljard/104/g; $line =~ s/over 1 miljardi/105/g; $line =~ s/1 - 5 emp/5/g; $line =~ s/6 - 15 emp/15/g; $line =~ s/16 - 25 emp/25/g; $line =~ s/26 - 50 emp/50/g; $line =~ s/51 - 100 emp/100/g; $line =~ s/101 - 250 emp/250/g; $line =~ s/251 - 500 emp/500/g; $line =~ s/501 - 1000 emp/1000/g; $line =~ s/over 1000 emp/1001/g; $line =~ s/;/|/g; # needs if or something $line =~ s/(.*\sAS)/1/g; # every other case should be 0 elseif !!! print $dest "$line"; } close($dest); close(IF); } close (FAILIDcnt); }

Replies are listed 'Best First'.
Re: substitute strings
by moritz (Cardinal) on May 12, 2010 at 10:25 UTC
    At the end of the line there should be added 1 more column - todays date "strftime(".%Y%m%d", localtime);" !!!

    Then do it!!!

    Or is there anything you are having problems with? If yes, what?

    At perlmonks we're willing to explain things if you have trouble, and help you to understand Perl and programming. Enhancing scripts for you is not our job.

    Perl 6 - links to (nearly) everything that is Perl 6.
      Thats done -$line =~ s/$/$add_date;/g; What I fail to do is add "0" where there is no value or the value does not match criteria and change column positions. >Enhancing scripts for you is not our job. did I ask that! No. I know what my script should do but I dont know how do to it right now.
        Do you know how to write a condition with if?

        Do you know how to match a regex without doing a substitution?

        Do you know how to extract certain fields with split?