LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   substitution.... (https://www.linuxquestions.org/questions/programming-9/substitution-616162/)

kadhan 01-24-2008 10:09 PM

substitution....
 
hi guys,
I have two files, ref.txt and replace.txt.
replace.txt has 500 lines.
I want to substitute a specific line in ref.txt with the 1st line of
replace.txt and save the new file as 1.txt...
similarly substitute that specific line in ref.txt with the 2nd line of
replace.txt and save the new file as 2.txt...
so finally i got 500 txt files.

Plz help me to write a script for doing this(perl , shell or sed anything is ok..)?


Plzzzz......

chrism01 01-25-2008 01:07 AM

Homework ?

kadhan 01-25-2008 08:30 AM

hey.... its not a homework??

zaichik 01-26-2008 08:31 AM

Quote:

I want to substitute a specific line
What is the specific line? Is it based on a pattern match, or is it a specific line number? In either event, is it based on user input, or is it okay to hard code the substition criterion?

angrybanana 01-26-2008 08:54 AM

Quote:

Originally Posted by kadhan (Post 3034524)
Plz help me to write a script for doing this(perl , shell or sed anything is ok..)?

I'm gonna go ahead and assume that means python is ok.
Code:

ref = open('ref.txt')
replace = open('replace.txt')

for i, rep_line in enumerate(replace):
    i += 1
    out = open('%s.txt' %i, 'w')
    for j, ref_line in enumerate(ref):
        j += 1
        #if j == 3:
        if ref_line.strip() == 'replace me':
            ref_line = rep_line
        out.write(ref_line)
    out.close()
    ref.seek(0)

output:
Code:

$ cat ref.txt
replace next line
replace me
replace line before
$ cat replace.txt
one
two
three
four
five
six
$ cat 2.txt
replace next line
two
replace line before

You didn't specify how you wanted to search for the line to be replaced, so I put in two ways.
Code:

if ref_line.strip() == 'replace me':
that replaces it based on line text
Code:

if j == 3:
that does it based on line number.
uncomment it and use it how you want.
if you want regex support then add in.
Code:

import re
#and use
if re.search('regex expr', ref_line):


kadhan 01-26-2008 11:30 AM

Hi
thanks for your support.

But actually i want the perl script.
But in my script, I cannot write the output to a file after substitution.

Plz help.....

regards
kadhan.

angrybanana 01-26-2008 12:52 PM

Quote:

Originally Posted by kadhan (Post 3035846)
Hi
thanks for your support.

But actually i want the perl script.
But in my script, I cannot write the output to a file after substitution.

Plz help.....

regards
kadhan.

A. What happened to shell and sed?
B. Since you have a script already, post it, and someone will help you out.

kadhan 01-27-2008 10:09 PM

Hi

sorry for the delay....
The code is given below...
Code:

#!/usr/bin/perl

$j=0;
unless (open(REPLACE, "replace.txt")) {
  die ("cannot open input file condition\n");
  }
  @input = <REPLACE>;
close (REPLACE);

for ($count = 0; $count <= 50; $count++) {
mkdir("$count", 0777)|| print $!;
#system("/bin/cp ref.txt $count$process[$j]/ref.txt");
open(MYFILE, "ref.txt") || die ("Could not open file");;
unless (open(OUTFILE, ">$count/outfile")) {
  die ("cannot open output file outfile\n");
  }

$line = <MYFILE>;
  while ($line ne "") {
  s/substitution/$input[$count]/;
  print OUTFILE ($line);
  $line = <MYFILE>;
  }

#while(<MYFILE>) {
#
#s/substitution/$input[$count]/;
#
# print $_;
#
#}

}

This script will create 50 directories.
and substitute the ref.txt file with the content in replace.txt.....
But i cannot write the file after substitution..
I am a newbie in perl... so plz help me.....

Disillusionist 01-28-2008 01:37 AM

Personally I would not create a numerical directory name, it is bad practice and can lead to unexpected issues.

Therefore I suggest the following two changes to your Perl script:

Code:

mkdir("dir_$count", 0777)|| print $!;
Code:

unless (open(OUTFILE, "> dir_$count/outfile")) {
  die ("cannot open output file outfile\n");
  }


kadhan 01-28-2008 04:17 AM

HI,

Thanks for ur suggestion...
But i dont get the answer for my question.....:cry:

osor 01-28-2008 11:20 AM

Quote:

Originally Posted by kadhan (Post 3037363)
But i dont get the answer for my question.....:cry:

You still haven’t answered many of the questions posed to you. In particular, which line or lines from ref.txt you would like to replace (this is not clear from your attempted script)? There are two possibilities I can think of: it is a constant line or it matches some regular expression. Also, it is not clear whether you want to replace the entire contents of the line or only the contents of the matched expression.

For example, suppose you want to replace an entire constant line (e.g., 42) of ref.txt. Then you could do something like this:
Code:

#!/usr/bin/perl

use constant LINE => 42; # The number of the specific line you want to replace

my ($ref, $rep) = do {
        open(my $ref, "ref.txt") && open(my $rep, "replace.txt") || die "$!";
        ([<$ref>], [<$rep>]);
};

for my $i (1..@{$rep}) {
        open(my $out, ">$i.txt") || die "Cannot create $i.txt: $!";
        $ref->[LINE-1] = $rep->[$i-1];
        print $out @{$ref};
}

If you want to replace the entirety of the line matching a regular expression, you could change the script to the following:
Code:

#!/usr/bin/perl

my ($ref, $rep) = do {
        open(my $ref, "ref.txt") && open(my $rep, "replace.txt") || die "$!";
        ([<$ref>], [<$rep>]);
};

my @nums=grep{$ref->[$_]=~/expression/}(0..@{$ref}-1);

for my $i (1..@{$rep}) {
        open(my $out, ">$i.txt") || die "Cannot create $i.txt: $!";
        $ref->[$_] = $rep->[$i-1] for @nums;
        print $out @{$ref};
}


archtoad6 01-28-2008 04:14 PM

Why mess around w/ Perl, when sed can do the job:
Code:

REF='ref.txt'
REP='replace.txt'
M=<line_#_in_ref.txt_to_be_replaced>
NEW='rep_file-'      # The invariant part of your produced files

NN=`cat $REP  | wc -l`
for N in `seq -w 1 $NN`
do
  LN=`sed -n "${N}p" $REP`
  sed "${M}c${LN}" $REF  > ${NEW}${N}.txt
done

10 lines of bash shell script, that could be reduced to 6 or even 4 if you're willing to hard code some things & accept a couple of ugly lines in the loop.

Like this:
Code:

for N in $(seq -w 1 `cat replace.txt  | wc -l`)
do
  sed "4c$(sed -n "${N}p" replace.txt)" ref.txt  > rep_file-${N}.txt
done

Note: The "4" in 'sed "4c' is the # of the line to replace from my test ref.txt.

Note 2: I did test the 1st example, but not the 2nd.

kadhan 01-28-2008 10:09 PM

Thanks....
 
Thanks......
Thanks .....


All times are GMT -5. The time now is 03:08 PM.