Jump to content


Photo

Insert text into a text file.


  • Please log in to reply
2 replies to this topic

#1 vidyashankara

vidyashankara
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 17 May 2006 - 10:27 PM

Hi, i want to add 4 letters to every line in a text file between the 73-76 column.
for example,
ATOM      1  N   ASP A   1      53.812 -11.138 126.976  1.00 27.73           N  
ATOM      2  CA  ASP A   1      54.887 -12.148 127.179  1.00 27.09           C  
ATOM      3  C   ASP A   1      55.387 -12.701 125.851  1.00 24.03           C  
ATOM      4  O   ASP A   1      56.597 -12.865 125.652  1.00 22.99           O  
ATOM      5  CB  ASP A   1      54.405 -13.282 128.081  1.00 29.92           C  

should become

ATOM      1  N   ASP A   1      53.812 -11.138 126.976  1.00 27.73      PRO0 N  
ATOM      2  CA  ASP A   1      54.887 -12.148 127.179  1.00 27.09      PRO0 C  
ATOM      3  C   ASP A   1      55.387 -12.701 125.851  1.00 24.03      PRO0 C  
ATOM      4  O   ASP A   1      56.597 -12.865 125.652  1.00 22.99      PRO0 O  
ATOM      5  CB  ASP A   1      54.405 -13.282 128.081  1.00 29.92      PRO0 C  

If there is some other text in that column, it should overwrite it. how do i go about doing this? Can i do it in PHP. if not can i do it with some system command?

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 17 May 2006 - 11:40 PM

There are a number ways of doing this in PHP.
  • Remember, strings can be treated as arrays, so you can do someting like:
    <?php
    $input = file('in.txt'); // read file into an array
    $fp = f open('out.txt','w'); // open output file for write 
    foreach($input as $line) {
          $line[72] = 'P'; // remember the first character of the string is at position 0
          $line[73] = 'R';
          $line[74] = 'O';
          $line[75] = '0';
          f write($fp,$line);
    }
    f close($fp);
    ?>
  • Use the substr() function
    <?php
    $input = file('in.txt'); // read file into an array
    $fp = f open('out.txt','w'); // open output file for write 
    foreach($input as $line) {
         $part1 = substr($line,0,72);
         $part2 = substr($line,76);
         f write($fp,$part1 . 'PRO0' . $part2);
    }
    f close($fp);
    ?>
  • You could probably use one of the regular expression replace functions, but they still baffle me. [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]
Note: please remove the space between the "f" and the rest of filesystem functions.
Note2: the code has not been tested.

Ken

#3 vidyashankara

vidyashankara
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 18 May 2006 - 02:28 PM

[!--quoteo(post=374815:date=May 17 2006, 06:40 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ May 17 2006, 06:40 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
There are a number ways of doing this in PHP.
  • Remember, strings can be treated as arrays, so you can do someting like:
    <?php
    $input = file('in.txt'); // read file into an array
    $fp = f open('out.txt','w'); // open output file for write 
    foreach($input as $line) {
          $line[72] = 'P'; // remember the first character of the string is at position 0
          $line[73] = 'R';
          $line[74] = 'O';
          $line[75] = '0';
          f write($fp,$line);
    }
    f close($fp);
    ?>
  • Use the substr() function
    <?php
    $input = file('in.txt'); // read file into an array
    $fp = f open('out.txt','w'); // open output file for write 
    foreach($input as $line) {
         $part1 = substr($line,0,72);
         $part2 = substr($line,76);
         f write($fp,$part1 . 'PRO0' . $part2);
    }
    f close($fp);
    ?>
  • You could probably use one of the regular expression replace functions, but they still baffle me. [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]
Note: please remove the space between the "f" and the rest of filesystem functions.
Note2: the code has not been tested.

Ken
[/quote]


Thats a great idea! I will check it :) i never thought of it.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users