#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    12
    Rep Power
    0

    Filtering number from a column


    Hi everybody,
    I need some help to complete this part of my project. I have a file of many columns. The 3 column is energy distribution and I would like to read and output any value greater than -55 and less than -51. I tried this script which output nothing. Can someone help me. Here is my script:

    #!/usr/bin/perl

    use strict;
    my $l;
    my $col;

    open (FILE, $ARGV[0]);
    my @file = <FILE>;
    foreach my $l (@file){
    chomp($l);

    if($l<-55 && $l>-51){
    my @line = split (/\s+/,$l);

    $col = $line[3];
    print "$col\n";
    }
    }
    close(FILE);


    And here portion of the file I am trying to read. This shows some of the numbers as each column really has at least 50000 to 70000 values. I can not copy all to the question :
    12209.897 0.276 -44.3055 17.8725756285 -0.0000000003 1.790000 31 25.017901 18 2
    12219.922 0.285 -43.7370 17.5843109145 0.0000000022 0.500000 31 25.017901 18 2
    12229.957 0.279 -42.0493 17.1084744259 -0.0000000061 -0.832500 32 25.017901 18 2
    12239.904 0.291 -40.9408 17.1387521823 0.0000000085 -2.630000 29 25.017901 18 2
    12249.877 0.290 -42.2985 17.1688255419 0.0000000086 0.697500 31 25.017901 18 2
    12259.874 0.274 -41.5913 17.7556083051 -0.0000000062 0.130000 30 25.017901 18 2
    12269.884 0.280 -42.3440 17.6969750671 -0.0000000009 2.875000 32 25.017901 18 2
    12279.852 0.286 -41.1593 17.7415935314 -0.0000000015 6.830000 30 25.017901 18 2
    12289.929 0.286 -42.5345 17.7707233503 0.0000000004 4.230000 30 25.017901 18 2
    12299.883 0.268 -39.9593 17.4623484627 -0.0000000062 2.710000 31 25.017901 18 2
    12309.923 0.258 -42.2878 17.3196203889 0.0000000091 -0.570000 30 25.017901 18 2
    12319.963 0.274 -42.1665 17.3363817130 -0.0000000102 0.845000 31 25.017901 18 2
    12329.988 0.293 -41.6913 17.8223704373 0.0000000072 0.550000 30 25.017901 18 2
    12339.897 0.283 -41.7555 17.7513009354 -0.0000000064 -0.090000 33 25.017901 18 2
    12349.865 0.250 -42.0503 17.8341096810 0.0000000023 0.430000 31 25.017901 18 2
    12359.867 0.261 -43.3498 17.8856463750 0.0000000011 -4.440000 31 25.017901 18 2
    12369.911 0.260 -41.6150 18.0463290163 -0.0000000150 1.125000 29 25.017901 18 2
    12379.948 0.258 -43.1020 17.9668716547 -0.0000000013 -0.620000 29 25.017901 18 2
    12389.916 0.273 -42.2848 18.1429402314 -0.0000000031 -0.432500 30 25.017901 18 2
    12399.880 0.266 -42.6658 18.4155943868 -0.0000000121 -0.755000 31 25.017901 18 2
    12409.996 0.270 -40.9660 18.6535750610 0.0000000137 1.772500 32 25.017901 18 2
    12419.945 0.267 -42.5380 18.6404495530 0.0000000058 6.365000 32 25.017901 18 2
    12429.856 0.276 -40.3040 18.5098994758 0.0000000021 6.700000 31 25.017901 18 2
    12439.871 0.285 -41.9955 18.3135570846 0.0000000041 2.570000 31 25.017901 18 2
    12449.983 0.287 -43.3635 18.3597154550 -0.0000000047 -2.780000 31 25.017901 18 2
    12459.848 0.276 -40.4188 18.4208922186 0.0000000033 1.210000 32 25.017901 18 2
    12469.972 0.263 -42.7980 18.2938833728 0.0000000002 6.250000 30 25.017901 18 2
    12479.932 0.285 -42.8213 18.14

    Thanks in advance for all the help.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,922
    Rep Power
    1225
    None of the column 3 values in the sample data you posted fall within your desired range.

    Try this test script. (note that I adjusted 2 rows to fall within your range)
    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    while (<DATA>) {
        my ($col3, $col4) = (split /\s+/)[2,3];
        if ($col3 > -55 and $col3 < -51) {
            print "$col4\n";
        }
    }
    
    
    __DATA__
    12209.897 0.276 -44.3055 17.8725756285 -0.0000000003 1.790000 31 25.017901 18 2
    12219.922 0.285 -43.7370 17.5843109145 0.0000000022 0.500000 31 25.017901 18 2
    12229.957 0.279 -52.0493 17.1084744259 -0.0000000061 -0.832500 32 25.017901 18 2
    12239.904 0.291 -40.9408 17.1387521823 0.0000000085 -2.630000 29 25.017901 18 2
    12249.877 0.290 -42.2985 17.1688255419 0.0000000086 0.697500 31 25.017901 18 2
    12259.874 0.274 -51.5913 17.7556083051 -0.0000000062 0.130000 30 25.017901 18 2
    12269.884 0.280 -42.3440 17.6969750671 -0.0000000009 2.875000 32 25.017901 18 2
    12279.852 0.286 -41.1593 17.7415935314 -0.0000000015 6.830000 30 25.017901 18 2
    12289.929 0.286 -42.5345 17.7707233503 0.0000000004 4.230000 30 25.017901 18 2
    12299.883 0.268 -39.9593 17.4623484627 -0.0000000062 2.710000 31 25.017901 18 2
    12309.923 0.258 -42.2878 17.3196203889 0.0000000091 -0.570000 30 25.017901 18 2
    12319.963 0.274 -42.1665 17.3363817130 -0.0000000102 0.845000 31 25.017901 18 2
    12329.988 0.293 -41.6913 17.8223704373 0.0000000072 0.550000 30 25.017901 18 2
    12339.897 0.283 -41.7555 17.7513009354 -0.0000000064 -0.090000 33 25.017901 18 2
    12349.865 0.250 -42.0503 17.8341096810 0.0000000023 0.430000 31 25.017901 18 2
    12359.867 0.261 -43.3498 17.8856463750 0.0000000011 -4.440000 31 25.017901 18 2
    12369.911 0.260 -41.6150 18.0463290163 -0.0000000150 1.125000 29 25.017901 18 2
    12379.948 0.258 -43.1020 17.9668716547 -0.0000000013 -0.620000 29 25.017901 18 2
    12389.916 0.273 -42.2848 18.1429402314 -0.0000000031 -0.432500 30 25.017901 18 2
    12399.880 0.266 -42.6658 18.4155943868 -0.0000000121 -0.755000 31 25.017901 18 2
    12409.996 0.270 -40.9660 18.6535750610 0.0000000137 1.772500 32 25.017901 18 2
    12419.945 0.267 -42.5380 18.6404495530 0.0000000058 6.365000 32 25.017901 18 2
    12429.856 0.276 -40.3040 18.5098994758 0.0000000021 6.700000 31 25.017901 18 2
    12439.871 0.285 -41.9955 18.3135570846 0.0000000041 2.570000 31 25.017901 18 2
    12449.983 0.287 -43.3635 18.3597154550 -0.0000000047 -2.780000 31 25.017901 18 2
    12459.848 0.276 -40.4188 18.4208922186 0.0000000033 1.210000 32 25.017901 18 2
    12469.972 0.263 -42.7980 18.2938833728 0.0000000002 6.250000 30 25.017901 18 2
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    12
    Rep Power
    0
    Originally Posted by FishMonger
    None of the column 3 values in the sample data you posted fall within your desired range.

    Try this test script. (note that I adjusted 2 rows to fall within your range)
    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    while (<DATA>) {
        my ($col3, $col4) = (split /\s+/)[2,3];
        if ($col3 > -55 and $col3 < -51) {
            print "$col4\n";
        }
    }
    
    
    __DATA__
    12209.897 0.276 -44.3055 17.8725756285 -0.0000000003 1.790000 31 25.017901 18 2
    12219.922 0.285 -43.7370 17.5843109145 0.0000000022 0.500000 31 25.017901 18 2
    12229.957 0.279 -52.0493 17.1084744259 -0.0000000061 -0.832500 32 25.017901 18 2
    12239.904 0.291 -40.9408 17.1387521823 0.0000000085 -2.630000 29 25.017901 18 2
    12249.877 0.290 -42.2985 17.1688255419 0.0000000086 0.697500 31 25.017901 18 2
    12259.874 0.274 -51.5913 17.7556083051 -0.0000000062 0.130000 30 25.017901 18 2
    12269.884 0.280 -42.3440 17.6969750671 -0.0000000009 2.875000 32 25.017901 18 2
    12279.852 0.286 -41.1593 17.7415935314 -0.0000000015 6.830000 30 25.017901 18 2
    12289.929 0.286 -42.5345 17.7707233503 0.0000000004 4.230000 30 25.017901 18 2
    12299.883 0.268 -39.9593 17.4623484627 -0.0000000062 2.710000 31 25.017901 18 2
    12309.923 0.258 -42.2878 17.3196203889 0.0000000091 -0.570000 30 25.017901 18 2
    12319.963 0.274 -42.1665 17.3363817130 -0.0000000102 0.845000 31 25.017901 18 2
    12329.988 0.293 -41.6913 17.8223704373 0.0000000072 0.550000 30 25.017901 18 2
    12339.897 0.283 -41.7555 17.7513009354 -0.0000000064 -0.090000 33 25.017901 18 2
    12349.865 0.250 -42.0503 17.8341096810 0.0000000023 0.430000 31 25.017901 18 2
    12359.867 0.261 -43.3498 17.8856463750 0.0000000011 -4.440000 31 25.017901 18 2
    12369.911 0.260 -41.6150 18.0463290163 -0.0000000150 1.125000 29 25.017901 18 2
    12379.948 0.258 -43.1020 17.9668716547 -0.0000000013 -0.620000 29 25.017901 18 2
    12389.916 0.273 -42.2848 18.1429402314 -0.0000000031 -0.432500 30 25.017901 18 2
    12399.880 0.266 -42.6658 18.4155943868 -0.0000000121 -0.755000 31 25.017901 18 2
    12409.996 0.270 -40.9660 18.6535750610 0.0000000137 1.772500 32 25.017901 18 2
    12419.945 0.267 -42.5380 18.6404495530 0.0000000058 6.365000 32 25.017901 18 2
    12429.856 0.276 -40.3040 18.5098994758 0.0000000021 6.700000 31 25.017901 18 2
    12439.871 0.285 -41.9955 18.3135570846 0.0000000041 2.570000 31 25.017901 18 2
    12449.983 0.287 -43.3635 18.3597154550 -0.0000000047 -2.780000 31 25.017901 18 2
    12459.848 0.276 -40.4188 18.4208922186 0.0000000033 1.210000 32 25.017901 18 2
    12469.972 0.263 -42.7980 18.2938833728 0.0000000002 6.250000 30 25.017901 18 2
    I appreciate your help FishMonger. Thanks for your time.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    12
    Rep Power
    0

    Hi Mr FishMonger


    I have tried the script your help me with but It is not working. It is not outputting anything. Do you know any alternative way. Thanks a lot.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,922
    Rep Power
    1225
    Are you saying the test script I provided isn't outputting anything, or that it doesn't output anything when you apply the loop to your actual data?

    If it fails with your actual data, then obviously that data is not formatted the same as the sample you posted or none of the values in column 3 fall within your range.

    Comments on this post

    • Laurent_R agrees : Quite obviously, FishMonger's script should work with data similar to what has been posted. If it does not, then the data is probably different.

IMN logo majestic logo threadwatch logo seochat tools logo