SlideShare ist ein Scribd-Unternehmen logo
1 von 5
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Andere mochten auch

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
 

Andere mochten auch (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
 

Kürzlich hochgeladen

Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 

Kürzlich hochgeladen (20)

Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 

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