Jump to content

Reading Text File, Only certain columns...


vidyashankara

Recommended Posts

Here's a Text File.
[code]
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#
[/code]

@#!@# can be any number/letter or anything.

The script should read this file, come up with this output
The file has 2 unique HETATM's, they are HOH and AC9. Some text files might have just HOH or just AC9 or neither, They might have something else. Any way out?
Link to comment
https://forums.phpfreaks.com/topic/11591-reading-text-file-only-certain-columns/
Share on other sites

I believe I coded something like that for you before. It is here:
[a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=95300\" target=\"_blank\"]http://www.phpfreaks.com/forums/index.php?showtopic=95300[/a]

A slight modification:

[code]<?php

$str = 'HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#';

// If you want to retrieve the values from a file, use this:
// $str = file_get_contents('file.txt');

$array = explode("\n", $str);

for ($i=0; $i<count($array); $i++) {
   preg_match_all("/([^ \r\n]+)(?:[ ]+)*/", $array[$i], $m);
   $matches[$i] = $m[1];
   $fifth[$i] = $m[1][4];
}

echo '<pre>';
print_r($matches);
echo '</pre>';

?>[/code]

$matches is an array and should be something like this:

[code]Array
(
    [0] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => HOH
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [1] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => HOH
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [2] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => HOH
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [3] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => HOH
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [4] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => HOH
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [5] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => HOH
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [6] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => HOH
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [7] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => AC9
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [8] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => AC9
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [9] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => AC9
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [10] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => AC9
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [11] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => AC9
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

)[/code]

What is easy enough to work with I believe.
[!--quoteo(post=381936:date=Jun 9 2006, 12:47 PM:name=poirot)--][div class=\'quotetop\']QUOTE(poirot @ Jun 9 2006, 12:47 PM) [snapback]381936[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I believe I coded something like that for you before. It is here:
[a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=95300\" target=\"_blank\"]http://www.phpfreaks.com/forums/index.php?showtopic=95300[/a]

A slight modification:

[code]<?php

$str = 'HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#';

// If you want to retrieve the values from a file, use this:
// $str = file_get_contents('file.txt');

$array = explode("\n", $str);

for ($i=0; $i<count($array); $i++) {
   preg_match_all("/([^ \r\n]+)(?:[ ]+)*/", $array[$i], $m);
   $matches[$i] = $m[1];
   $fifth[$i] = $m[1][4];
}

echo '<pre>';
print_r($matches);
echo '</pre>';

?>[/code]

$matches is an array and should be something like this:

[code]Array
(
    [0] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => HOH
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [1] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => HOH
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [2] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => HOH
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [3] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => HOH
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [4] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => HOH
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [5] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => HOH
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [6] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => HOH
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [7] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => AC9
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [8] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => AC9
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [9] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => AC9
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [10] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => AC9
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [11] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => AC9
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

)[/code]

What is easy enough to work with I believe.
[/quote]


Yeah the 2 scripts are similar. I couldnt figure out how to modify it. The last one was just one letter. I figured a 3 letter word might be complex.




Hey, Thanks! I really appreciate your help! :) you rock!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.