Jump to content

Recommended Posts

Hi, i want to add 4 letters to every line in a text file between the 73-76 column.
for example,
[code]
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  
[/code]

should become

[code]
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  
[/code]

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?
Link to comment
https://forums.phpfreaks.com/topic/9886-insert-text-into-a-text-file/
Share on other sites

There are a number ways of doing this in PHP.[list][*]Remember, strings can be treated as arrays, so you can do someting like:
[code]<?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);
?>[/code][*]Use the substr() function
[code]<?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);
?>[/code][*]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\" /][/list]Note: please remove the space between the "f" and the rest of filesystem functions.
Note2: the code has not been tested.

Ken
[!--quoteo(post=374815:date=May 17 2006, 06:40 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ May 17 2006, 06:40 PM) [snapback]374815[/snapback][/div][div class=\'quotemain\'][!--quotec--]
There are a number ways of doing this in PHP.[list]
[*]Remember, strings can be treated as arrays, so you can do someting like:
[code]<?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);
?>[/code]
[*]Use the substr() function
[code]<?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);
?>[/code]
[*]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\" /]
[/list]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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.