LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-24-2008, 10:09 PM   #1
kadhan
Member
 
Registered: Dec 2004
Posts: 40

Rep: Reputation: 15
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......
 
Old 01-25-2008, 01:07 AM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,283

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Homework ?
 
Old 01-25-2008, 08:30 AM   #3
kadhan
Member
 
Registered: Dec 2004
Posts: 40

Original Poster
Rep: Reputation: 15
hey.... its not a homework??
 
Old 01-26-2008, 08:31 AM   #4
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Rep: Reputation: 30
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?
 
Old 01-26-2008, 08:54 AM   #5
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Quote:
Originally Posted by kadhan View Post
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):
 
Old 01-26-2008, 11:30 AM   #6
kadhan
Member
 
Registered: Dec 2004
Posts: 40

Original Poster
Rep: Reputation: 15
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.
 
Old 01-26-2008, 12:52 PM   #7
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Quote:
Originally Posted by kadhan View Post
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.
 
Old 01-27-2008, 10:09 PM   #8
kadhan
Member
 
Registered: Dec 2004
Posts: 40

Original Poster
Rep: Reputation: 15
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.....
 
Old 01-28-2008, 01:37 AM   #9
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
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");
  }
 
Old 01-28-2008, 04:17 AM   #10
kadhan
Member
 
Registered: Dec 2004
Posts: 40

Original Poster
Rep: Reputation: 15
HI,

Thanks for ur suggestion...
But i dont get the answer for my question.....
 
Old 01-28-2008, 11:20 AM   #11
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Quote:
Originally Posted by kadhan View Post
But i dont get the answer for my question.....
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};
}
 
Old 01-28-2008, 04:14 PM   #12
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
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.
 
Old 01-28-2008, 10:09 PM   #13
kadhan
Member
 
Registered: Dec 2004
Posts: 40

Original Poster
Rep: Reputation: 15
Wink Thanks....

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
substitution ovince Programming 3 04-28-2007 05:35 AM
Substitution in Perl rigel_kent Programming 4 06-02-2006 10:11 AM
substitution with a function rigel_kent Programming 4 05-20-2006 05:28 PM
command substitution: ^ rhxk Linux - General 2 04-06-2006 09:51 AM
Is there any substitution to Ghost? Swift&Smart Linux - Software 12 06-22-2004 05:31 PM


All times are GMT -5. The time now is 09:33 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration