SlideShare uma empresa Scribd logo
1 de 5
Baixar para ler offline
Converting a text file into a CSV file
  with an additional column/field
 The first 11 lines of source text file
                     look like this -->   1990
                                          41502   Year


                                          1991
 The objective is to
 transform the file into a flat
                                          41820
 database file containing                           Village
 the following columns:                   1992      Count
 Year, Count, and                         41876
 Variance. Variance is the
 difference between the                   1993
 count in a row and that in
 the previous row.                        41931
Perl Source Code
#!/usr/bin/perl

use strict;

my $year;
my $brgy_count_1 = 0;
my $brgy_count_2 = 0;
my $counter = 1;
my $first_line = 1;

open SOURCE_FILE, quot;/path/to/source_filequot;;
open TARGET_FILE, quot;>/path/to/target_filequot;;
Perl Source Code (2)
while (<SOURCE_FILE>) {
  next if ($_ =~ m/^$/);
  if ($counter == 1 or $counter == 3) {
      $_ =~ m/(d{4})/;
      print TARGET_FILE $1 . quot;tquot;;
      $counter += 1;
  } else {
      $_ =~ m/(d+)$/;
      if ($first_line == 0) {
          $brgy_count_2 = $1;
      }
      print TARGET_FILE $1 . quot;tquot;.
      ($brgy_count_2 - $brgy_count_1) . quot;nquot;;
Perl Source Code (3)
        if ($first_line == 1) { $brgy_count_1 = $1;
        } else { $brgy_count_1 = $brgy_count_2; }
        if ($counter == 4) {
            $counter = 1;
        } else {
            $counter += 1;
        }
        $first_line = 0;
    }
}

close SOURCE_FILE;
close TARGET_FILE;
The new flat database file
The first 10 lines of output file
                look like this -->   1990   41502     0
                                     1991   41820     318
                                     1992   41876     56
The output file uses a
                                     1993   41931     55
tab as field/column                  1994   41919     -12
separator. To use a                  1995   41929     10
comma, just go to the                1996   41935     6
related code's line                  1997   41939     4
and change the                       1998   41940     1
separator from “t” to
“,”.                                 1999   41940     0

                                            Village
                              Year                      Variance
                                            Count

Mais conteúdo relacionado

Destaque

Educational System in the Philippines, Quality Education and Access to Education
Educational System in the Philippines, Quality Education and Access to EducationEducational System in the Philippines, Quality Education and Access to Education
Educational System in the Philippines, Quality Education and Access to EducationRose Ann Enriquez
 
Historical perspective of the philippine educational system lee ann
Historical perspective of the philippine educational system lee annHistorical perspective of the philippine educational system lee ann
Historical perspective of the philippine educational system lee annJerson Panopio
 
K to 12 electrical teacher's guide
K to 12 electrical teacher's guideK to 12 electrical teacher's guide
K to 12 electrical teacher's guideNoel Tan
 
The Organizational Structure of the Philippine Educational System
The Organizational Structure of the Philippine Educational SystemThe Organizational Structure of the Philippine Educational System
The Organizational Structure of the Philippine Educational SystemGlance Ruiz
 
Philippine education presentation
Philippine education presentationPhilippine education presentation
Philippine education presentationCarlo Magno
 
K to 12 bread and pastry teacher's guide
K to 12 bread and pastry teacher's guideK to 12 bread and pastry teacher's guide
K to 12 bread and pastry teacher's guideNoel Tan
 
Problems and Issues in the Philippine Educational System
Problems and Issues in the Philippine Educational SystemProblems and Issues in the Philippine Educational System
Problems and Issues in the Philippine Educational SystemJames Paglinawan
 
Education System of the Philippines
Education System of the PhilippinesEducation System of the Philippines
Education System of the PhilippinesCarms Celis
 
DepEd, CHED and TESDA
DepEd, CHED and TESDADepEd, CHED and TESDA
DepEd, CHED and TESDArajnulada
 
K to 12 General Presentation
K to 12 General PresentationK to 12 General Presentation
K to 12 General PresentationDepEdPhilippines
 
K to 12 classroom assessment ppt
K to 12 classroom assessment pptK to 12 classroom assessment ppt
K to 12 classroom assessment pptCarlo Magno
 
Historical foundation of philippine education
Historical foundation of philippine education Historical foundation of philippine education
Historical foundation of philippine education Michael John Labog
 
K 12 basic education program
K 12 basic education programK 12 basic education program
K 12 basic education program19710802
 

Destaque (15)

Educational System in the Philippines, Quality Education and Access to Education
Educational System in the Philippines, Quality Education and Access to EducationEducational System in the Philippines, Quality Education and Access to Education
Educational System in the Philippines, Quality Education and Access to Education
 
Historical perspective of the philippine educational system lee ann
Historical perspective of the philippine educational system lee annHistorical perspective of the philippine educational system lee ann
Historical perspective of the philippine educational system lee ann
 
K to 12 electrical teacher's guide
K to 12 electrical teacher's guideK to 12 electrical teacher's guide
K to 12 electrical teacher's guide
 
The Organizational Structure of the Philippine Educational System
The Organizational Structure of the Philippine Educational SystemThe Organizational Structure of the Philippine Educational System
The Organizational Structure of the Philippine Educational System
 
Philippine education presentation
Philippine education presentationPhilippine education presentation
Philippine education presentation
 
K to 12 bread and pastry teacher's guide
K to 12 bread and pastry teacher's guideK to 12 bread and pastry teacher's guide
K to 12 bread and pastry teacher's guide
 
Problems and Issues in the Philippine Educational System
Problems and Issues in the Philippine Educational SystemProblems and Issues in the Philippine Educational System
Problems and Issues in the Philippine Educational System
 
Education System of the Philippines
Education System of the PhilippinesEducation System of the Philippines
Education System of the Philippines
 
Deped K12
Deped K12Deped K12
Deped K12
 
DepEd, CHED and TESDA
DepEd, CHED and TESDADepEd, CHED and TESDA
DepEd, CHED and TESDA
 
K to 12 General Presentation
K to 12 General PresentationK to 12 General Presentation
K to 12 General Presentation
 
K to 12 Science Curriculum Guide
K to 12  Science Curriculum GuideK to 12  Science Curriculum Guide
K to 12 Science Curriculum Guide
 
K to 12 classroom assessment ppt
K to 12 classroom assessment pptK to 12 classroom assessment ppt
K to 12 classroom assessment ppt
 
Historical foundation of philippine education
Historical foundation of philippine education Historical foundation of philippine education
Historical foundation of philippine education
 
K 12 basic education program
K 12 basic education programK 12 basic education program
K 12 basic education program
 

Último

SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 

Último (20)

SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 

Convert text file to CSV with additional column for variance

  • 1. Converting a text file into a CSV file with an additional column/field The first 11 lines of source text file look like this --> 1990 41502 Year 1991 The objective is to transform the file into a flat 41820 database file containing Village the following columns: 1992 Count Year, Count, and 41876 Variance. Variance is the difference between the 1993 count in a row and that in the previous row. 41931
  • 2. Perl Source Code #!/usr/bin/perl use strict; my $year; my $brgy_count_1 = 0; my $brgy_count_2 = 0; my $counter = 1; my $first_line = 1; open SOURCE_FILE, quot;/path/to/source_filequot;; open TARGET_FILE, quot;>/path/to/target_filequot;;
  • 3. Perl Source Code (2) while (<SOURCE_FILE>) { next if ($_ =~ m/^$/); if ($counter == 1 or $counter == 3) { $_ =~ m/(d{4})/; print TARGET_FILE $1 . quot;tquot;; $counter += 1; } else { $_ =~ m/(d+)$/; if ($first_line == 0) { $brgy_count_2 = $1; } print TARGET_FILE $1 . quot;tquot;. ($brgy_count_2 - $brgy_count_1) . quot;nquot;;
  • 4. Perl Source Code (3) if ($first_line == 1) { $brgy_count_1 = $1; } else { $brgy_count_1 = $brgy_count_2; } if ($counter == 4) { $counter = 1; } else { $counter += 1; } $first_line = 0; } } close SOURCE_FILE; close TARGET_FILE;
  • 5. The new flat database file The first 10 lines of output file look like this --> 1990 41502 0 1991 41820 318 1992 41876 56 The output file uses a 1993 41931 55 tab as field/column 1994 41919 -12 separator. To use a 1995 41929 10 comma, just go to the 1996 41935 6 related code's line 1997 41939 4 and change the 1998 41940 1 separator from “t” to “,”. 1999 41940 0 Village Year Variance Count