Sponsored Content
Full Discussion: Turning files into an array
Top Forums Shell Programming and Scripting Turning files into an array Post 302297699 by ldapswandog on Saturday 14th of March 2009 03:59:00 PM
Old 03-14-2009
Here is a solution in Perl which is more efficient than just using shell
Code:
#!/usr/bin/perl -w

# # set variables
@files="A B C";

# # open output files
open (GOOD, "> good.txt");
open (BAD, "> bad.txt");
open (FAIR, "> fair.txt");

# # for each file
while ($file = <@files>) {
  # # open file
  open (INPUT, "< $file");
  # # process each line of file
  while ($line = <INPUT>) {
    chomp $line;
    # # IF line contain strawberries
    if ($line =~ m/Strawberry/) {
      # # Split line into each value
      ($name,$good,$bad,$fair) = split (/ /, $line);
      if ($file =~ m/A/) {
        $sA_good=$good;
        $sA_bad=$bad;
        $sA_fair=$fair;
      }
      elsif ($file =~ m/B/) {
        $sB_good=$good;
        $sB_bad=$bad;
        $sB_fair=$fair;
      }
      elsif ($file =~ m/C/) {
        $sC_good=$good;
        $sC_bad=$bad;
        $sC_fair=$fair;
      }
    }
    # # ELSE IF line contain bananas
    elsif ($line =~ m/Banana/) {
      # # Split line into each value
      ($name,$good,$bad,$fair) = split (/ /, $line);
      if ($file =~ m/A/) {
        $bA_good=$good;
        $bA_bad=$bad;
        $bA_fair=$fair;
      }
      elsif ($file =~ m/B/) {
        $bB_good=$good;
        $bB_bad=$bad;
        $bB_fair=$fair;
      }
      elsif ($file =~ m/C/) {
        $bC_good=$good;
        $bC_bad=$bad;
        $bC_fair=$fair;
      }
    }
    # # ELSE IF line contain plantains
    elsif ($line =~ m/Plantain/) {
      # # Split line into each value
      ($name,$good,$bad,$fair) = split (/ /, $line);
      if ($file =~ m/A/) {
        $pA_good=$good;
        $pA_bad=$bad;
        $pA_fair=$fair;
      }
      elsif ($file =~ m/B/) {
        $pB_good=$good;
        $pB_bad=$bad;
        $pB_fair=$fair;
      }
      elsif ($file =~ m/C/) {
        $pC_good=$good;
        $pC_bad=$bad;
        $pC_fair=$fair;
      }
    }
    # # ELSE IF line contain oranges
    elsif ($line =~ m/Orange/) {
      # # Split line into each value
      ($name,$good,$bad,$fair) = split (/ /, $line);
      if ($file =~ m/A/) {
        $oA_good=$good;
        $oA_bad=$bad;
        $oA_fair=$fair;
      }
      elsif ($file =~ m/B/) {
        $oB_good=$good;
        $oB_bad=$bad;
        $oB_fair=$fair;
      }
      elsif ($file =~ m/C/) {
        $oC_good=$good;
        $oC_bad=$bad;
        $oC_fair=$fair;
      }
    }
  }
}

# # calc totals
$tgA=($sA_good + $bA_good + $pA_good + $oA_good);
$tgB=($sB_good + $bB_good + $pB_good + $oB_good);
$tgC=($sC_good + $bC_good + $pC_good + $oC_good);
$tbA=($sA_bad + $bA_bad + $pA_bad + $oA_bad);
$tbB=($sB_bad + $bB_bad + $pB_bad + $oB_bad);
$tbC=($sC_bad + $bC_bad + $pC_bad + $oC_bad);
$tfA=($sA_fair + $bA_fair + $pA_fair + $oA_fair);
$tfB=($sB_fair + $bB_fair + $pB_fair + $oB_fair);
$tfC=($sC_fair + $bC_fair + $pC_fair + $oC_fair);

# # print data to output files
print GOOD "GOOD\n";
print GOOD "A B C\n";
print GOOD "Strawberry\t$sA_good\t$sB_good\t$sC_good\n";
print GOOD "Banana    \t$bA_good\t$bB_good\t$bC_good\n";
print GOOD "Plantain  \t$pA_good\t$pB_good\t$pC_good\n";
print GOOD "Orange    \t$oA_good\t$oB_good\t$oC_good\n";
print GOOD "===================================\n";
print GOOD "Totals    \t$tgA\t$tgB\t$tgC\n";

print BAD "BAD\n";
print BAD "A B C\n";
print BAD "Strawberry\t$sA_bad\t$sB_bad\t$sC_bad\n";
print BAD "Banana    \t$bA_bad\t$bB_bad\t$bC_bad\n";
print BAD "Plantain  \t$pA_bad\t$pB_bad\t$pC_bad\n";
print BAD "Orange    \t$oA_bad\t$oB_bad\t$oC_bad\n";
print BAD "===================================\n";
print BAD "Totals    \t$tbA\t$tbB\t$tbC\n";

print FAIR "FAIR\n";
print FAIR "A B C\n";
print FAIR "Strawberry\t$sA_fair\t$sB_fair\t$sC_fair\n";
print FAIR "Banana    \t$bA_fair\t$bB_fair\t$bC_fair\n";
print FAIR "Plantain  \t$pA_fair\t$pB_fair\t$pC_fair\n";
print FAIR "Orange    \t$oA_fair\t$oB_fair\t$oC_fair\n";
print FAIR "===================================\n";
print FAIR "Totals    \t$tfA\t$tfB\t$tfC\n";

# # close files
close (GOOD);
close (BAD);
close (FAIR);
close (INPUT);

exit 0;

 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Turning off the CDE

I am running Solaris 9 and wanted the CDE stopped when my users login. Can this be done by adding something to the .profile? Basically when they login they should be at the command line and have to start the CDE themselves. Thanks (11 Replies)
Discussion started by: meyersp
11 Replies

2. Gentoo

Turning on/off the network interface

Hi all, I'm trying to write a script that will turn off the network interface eth0 on a linux Gentoo machine and then turn it back on, any help? Thanks, Neked (1 Reply)
Discussion started by: neked
1 Replies

3. UNIX for Dummies Questions & Answers

Turning Echo off

Hi, Is there any way like in dos to turn the echo off in a script? i have some lines popping up that i dont wish to be viewed when i am unziping a file it brings up the message updating: log.txt (deflated 72%) and extracting: log.txt i dont want these be viewed. Andy (4 Replies)
Discussion started by: chapmana
4 Replies

4. Solaris

Turning in.ftpd on and off

For two straight days someone was running in.ftpd in my server (apparently looking to break in) and when I would do "top" almost every line would read "in.ftpd". I had a unix sysadmin friend of mine shut it down and then start it back up in a day and a half and all seems OK for now. Here's what I... (1 Reply)
Discussion started by: thomi39
1 Replies

5. UNIX for Advanced & Expert Users

turning CIO on and how to monitor

Hi Guys, I have a database server where we run AIX 5.3 on a power5 box and we just turned on CIO (concurrent I/O) for the database filesystems. Now my assumption is that enabling CIO the database basically will bypass the filesystem cache releasing some extra memory that can be allocated... (1 Reply)
Discussion started by: hariza
1 Replies

6. AIX

turning CIO on and how to monitor

Hi Guys, I have a database server where we run AIX 5.3 on a power5 box and we just turned on CIO (concurrent I/O) for the database filesystems. Now my assumption is that enabling CIO the database basically will bypass the filesystem cache releasing some extra memory that can be allocated... (1 Reply)
Discussion started by: hariza
1 Replies

7. Shell Programming and Scripting

Turning CSV files into individual Variables

I want to be able to convert the following data from a CSV into individual variables from the columns 2 4 and 8 I can use awk to grab the columns using var1=`cat text.csv | awk "," '{print $2}'` but how do I create separate variables for each line. 595358 ,ECON1010 ,THU ,08:00 - 10:00 ,11 Mar... (6 Replies)
Discussion started by: domsmith
6 Replies

8. Shell Programming and Scripting

Comparing files in a directory against an array of files

I hope I can explain this correctly. I am using Bash-4.2 for my shell. I have a group of file names held in an array. I want to compare the names in this array against the names of files currently present in a directory. If the file does not exist in the directory, that is not a problem.... (5 Replies)
Discussion started by: BudMan
5 Replies

9. SCO

Need help turning off bootpd

OSR 5.0.7 patched with MP 5 The syslog is flooded with messages: May 9 13:42:12 asiwc bootpd: IP address not found: 192.168.230.215 May 9 13:42:13 asiwc bootpd: IP address not found: 192.168.230.142 May 9 13:42:50 asiwc bootpd: IP address not found: 192.168.230.202 The system... (4 Replies)
Discussion started by: migurus
4 Replies

10. Shell Programming and Scripting

Turning given date to epoch

i can probably script this in bash, but, i was wondering, does anyone know of a simple way to translate a given time to epoch? date -d@"29/Oct/2013:17:53:11" the user would specify the date: 29/Oct/2013:17:53:11 and the script will simply interpret that to epoch: 1348838383 (this is just... (4 Replies)
Discussion started by: SkySmart
4 Replies
All times are GMT -4. The time now is 06:11 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy