13 Replies - 5555 Views - Last Post: 15 September 2012 - 02:28 AM

#1 blindTiger  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 12-April 12

Replace Text in a file

Posted 12 September 2012 - 09:45 PM

I have a text file with a fixed number of characters per line. I need to read part of each line and substitute it with something else.

Example:
1234abc567
2345def678


I need to replace abc with something else. The new text might be variable length, but I need to put back 3 chars. For example if my replace string is just 'x', then I need to replace 'abc' with ' x'.

Does it make sense? Also, I need to replace some other parts of the line with spaces.

Any ideas on how I can accomplish this in C or a UNIX script?

thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Replace Text in a file

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1620
  • View blog
  • Posts: 3,078
  • Joined: 30-May 10

Re: Replace Text in a file

Posted 12 September 2012 - 10:09 PM

The generic text replacement Unix tool is sed

Eg
$ echo 1234abc567 | sed 's/abc/x/g'
1234x567


You can also do
sed -f script input-file > output-file
as well.
Was This Post Helpful? 0
  • +
  • -

#3 blindTiger  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 12-April 12

Re: Replace Text in a file

Posted 12 September 2012 - 10:13 PM

Thank you for your prompt reply. I think I need to explain better what I need to do.

The string will not always be the same, but will always be in the same position.

I would like to do it in the same file, for space issues.

I need to read characters 60 to 61 in the line, and then replace it with some other value.

I need to do this for each line.

thanks,
Was This Post Helpful? 0
  • +
  • -

#4 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1620
  • View blog
  • Posts: 3,078
  • Joined: 30-May 10

Re: Replace Text in a file

Posted 12 September 2012 - 10:22 PM

OK, so where is YOUR effort at solving this problem?

All I see is a load of "I want...." statements and not a lot of effort on your part at solving the problem.

This isn't a code on demand service - you need to show us what you can manage yourself.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3449
  • View blog
  • Posts: 10,643
  • Joined: 05-May 12

Re: Replace Text in a file

Posted 12 September 2012 - 10:30 PM

So what code or pseudo code do you have so far?
Was This Post Helpful? 0
  • +
  • -

#6 blindTiger  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 12-April 12

Re: Replace Text in a file

Posted 12 September 2012 - 10:32 PM

View PostSalem_c, on 12 September 2012 - 10:22 PM, said:

OK, so where is YOUR effort at solving this problem?

All I see is a load of "I want...." statements and not a lot of effort on your part at solving the problem.

This isn't a code on demand service - you need to show us what you can manage yourself.


You are right, I am sorry. Believe me I don't plan on just getting the work done for me. I am currently working on a C program, I am able to read the file but am having trouble reading only part of the line.
Was This Post Helpful? 0
  • +
  • -

#7 Ticon  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 320
  • Joined: 20-August 09

Re: Replace Text in a file

Posted 12 September 2012 - 11:31 PM

Please post your code using [ code] tags, so we can get a better view of what your trying to do
Was This Post Helpful? 0
  • +
  • -

#8 blindTiger  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 12-April 12

Re: Replace Text in a file

Posted 13 September 2012 - 09:00 AM

OK. I decided it might be easier to do a perl program. I have the code below, which reads a file and reads sub string from each line. I will get that string and go to a DB and pull a value from that DB, then I need to replace the part of the string where the original value was at. My problem is that I need to add spaces to the new value it is not of same length of the original value. is there any function for that, or do I need to write my own? My code currently adds the new value to the beginning of the line.

#!/bin/perl -w

use strict;

open(MYINPUTFILE, "<Test.txt");

my ($sakRecip, $replString);
$sakRecip = 12345;
$replString = ' ';

while(<MYINPUTFILE>) {
    
    my($line) = $_;
    
    chomp($line);
    
    if(length($line) < 36)
    {
       print "$line\n";
    }
    else
    {
        #$replString = printf '%12s', $sakRecip;
        substr($line, 671, 12) = '%12s', $sakRecip;
        print "$line\n";
    }
}


Was This Post Helpful? 0
  • +
  • -

#9 blindTiger  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 12-April 12

Re: Replace Text in a file

Posted 13 September 2012 - 09:09 AM

View PostblindTiger, on 13 September 2012 - 09:00 AM, said:

OK. I decided it might be easier to do a perl program. I have the code below, which reads a file and reads sub string from each line. I will get that string and go to a DB and pull a value from that DB, then I need to replace the part of the string where the original value was at. My problem is that I need to add spaces to the new value it is not of same length of the original value. is there any function for that, or do I need to write my own? My code currently adds the new value to the beginning of the line.

#!/bin/perl -w

use strict;

open(MYINPUTFILE, "<Test.txt");

my ($sakRecip, $replString);
$sakRecip = 12345;
$replString = ' ';

while(<MYINPUTFILE>) {
    
    my($line) = $_;
    
    chomp($line);
    
    if(length($line) < 36)
    {
       print "$line\n";
    }
    else
    {
        #$replString = printf '%12s', $sakRecip;
        substr($line, 671, 12) = '%12s', $sakRecip;
        print "$line\n";
    }
}



I was able to do that part.

I did
$replString = sprintf( '%12s', $sakRecip);
substr($line, 671, 12) = $replString;
print "$line\n";


Was This Post Helpful? 0
  • +
  • -

#10 blindTiger  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 12-April 12

Re: Replace Text in a file

Posted 13 September 2012 - 10:17 AM

My code is working now the way I need it to work.

Takes a file as input and outputs to standard output each line with the changes I need. If I pipe the output to a file I get a new file with the changes I need.

My question is: is there a way to do this in the same file instead of creating a new file?
Was This Post Helpful? 0
  • +
  • -

#11 hoople  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 8
  • Joined: 06-September 12

Re: Replace Text in a file

Posted 13 September 2012 - 02:33 PM

If you want to do this kind of record access in C you can just make a struct with three fixed sized char[].

Then random file access is simply a matter of seeking to the record number multiplied by the size of the struct then reading or writing, assuming the file is sized correctly.
Was This Post Helpful? 0
  • +
  • -

#12 blindTiger  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 12-April 12

Re: Replace Text in a file

Posted 13 September 2012 - 02:38 PM

View Posthoople, on 13 September 2012 - 02:33 PM, said:

If you want to do this kind of record access in C you can just make a struct with three fixed sized char[].

Then random file access is simply a matter of seeking to the record number multiplied by the size of the struct then reading or writing, assuming the file is sized correctly.



thanks for the tip.
Was This Post Helpful? 0
  • +
  • -

#13 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1620
  • View blog
  • Posts: 3,078
  • Joined: 30-May 10

Re: Replace Text in a file

Posted 13 September 2012 - 10:44 PM

If you do this
#!/bin/perl -w -i.bak

and delete this line
open(MYINPUTFILE, "<Test.txt");

change this to this
while(<MYINPUTFILE>) {
to this
while(<>) {


and run the program as this
perl myprog.pl Test.txt

Then you should find the file is modified in-place, and you have a Test.txt.bak file as the backup file.
Was This Post Helpful? 0
  • +
  • -

#14 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Replace Text in a file

Posted 15 September 2012 - 02:28 AM

View PostSalem_c, on 14 September 2012 - 06:44 AM, said:

If you do this
#!/bin/perl -w -i.bak
Then you should find the file is modified in-place, and you have a Test.txt.bak file as the backup file.


If you take a look at man perlrun and search down to the documentation of the -i switch, you will see that "-i renames or deletes the original file before creating a new file of the same name," which is what the OP is trying to avoid. It does not actually modify the original file in-place.

To the OP: There is a simpler solution which didn't occur to me in my earlier reply on the Perl forum, which would be to read the entire file into memory, close the file, do all your changes in-memory, and then overwrite the original file with them (or delete the original file and write a new one in its place). This would never have more than one (named[1]) copy of the file on-disk at a time. However, given that disk is much cheaper than memory and you seem to be concerned that having two copies of the file on-disk would take up too much space, I have a feeling that you're going to respond that you can't read the entire file into memory because you don't have enough memory to hold it...


[1] In most non-Microsoft environments, deleting a file while any process has it open only removes its name from the directory structure. The actual content of the file remains on disk until all processes using the file have closed it, so you could still temporarily have multiple copies of the file on disk even if you delete it.

This doesn't apply in Microsoft environments because they simply refuse to let you delete a file if any process has it open.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1