Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Substitution between two files

Status
Not open for further replies.

czarj

Technical User
Apr 22, 2004
130
US
Hi All,
Lets say I have two files and want to perform a substitution of the data in file1 based on the file2. Based on these initial two files I would like file1 to be changed to what is below. Note: file2 will be about 2500 lines.

Code:
__file1__
23
45
62
61
22

__file2__
23, joe
45, bob
62, pawan
61, kimber
22, sue-lee
..etc

Code:
__file1-revised__
joe
bob
pawan
kimber
sue-lee

Thanks in advance.


--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
Hi,

So what you wanna do, is load the 2 files, and then remove anything that occurs on the same line from file 1, and then save that output into a 3rd file?

For example

file 1 has:

some bla whatever

file 2 has:

abcdef1234 some bla whatever

..and file 3 would have "abcdef1234"

That correct?
 
Essentially yes.
Note: File1 is generated from a query (usually small) and file2 is static

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
I would first read File2 and create a hash
(23 => joe, 45 => bob, 62 => pawan, 61 => kimber, 22 => sue-lee, ..etc)
Then I would read File1 line by line and if line is hash key, then write the corresponding hash value into File3.
 
So I've decided to do this a slightly different way and only have one small problem. What i'm doing is reading the first value in a file (file1) and performing a query. If that query returns true I want to print out the entire line in the first file to a new one (file2). This process then repeats for each first entry in file1. I have the code written for everything, but instead of printing all the data from a file1 to file2, it is only printing the first value. I think the problem is related to the hash sorting, but i'm not sure what the solution is.

Code:
...variables get defined above...


open(INFO, $info_file) || die("Could not open file!");

my %infohash;
while (<INFO>)
{
   chomp;
   my ($key, $value) = split /\*/;
   $infohash{$key} .= exists $infohash{$key} ? ", $value" : $value;
}


open FILE, ">$filename.csv" or die $!;
print FILE "Serial,Name\n";

foreach $key (sort keys %infohash)
{
my $filvar = `$asupgrabloc -s $section -d $daterange $key`;
   if ($filvar =~ /$string/) {
   print FILE "$key , $value\n";
   }
}

What's happening is I get this:
1033953 ,
1033954 ,
1035478 ,

What I want to get is this (based on the contents of file 1):
1033953 , Joe Smith
1033954 , Bob Calyvas
1035478 , Kimber Sultana


Thanks!


--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
Given:
file1.txt
Code:
23
45
62
61
22
file2.txt
Code:
23, joe
45, bob
62, pawan
61, kimber
22, sue-lee
Then this program does the work
czarj.pl
Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$file1[/blue] = [red]'[/red][purple]file1.txt[/purple][red]'[/red][red];[/red]
[black][b]my[/b][/black] [blue]$file2[/blue] = [red]'[/red][purple]file2.txt[/purple][red]'[/red][red];[/red]
[black][b]my[/b][/black] [blue]$file3[/blue] = [red]'[/red][purple]file3.txt[/purple][red]'[/red][red];[/red]

[gray][i]# create hash from $file2[/i][/gray]
[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]1. Creating substitution hash from '[blue]$file2[/blue]'...[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red]FILE2, [blue]$file2[/blue][red])[/red]
  or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]'[/red][purple]Could not open $file2 for reading[/purple][red]'[/red][red];[/red]
[black][b]my[/b][/black] [blue]%subst_hash[/blue] = [red]([/red][red])[/red][red];[/red]
[olive][b]while[/b][/olive] [red]([/red] [black][b]my[/b][/black] [blue]$line[/blue] = <FILE2> [red])[/red] [red]{[/red]
  [url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red]([/red][blue]$line[/blue][red])[/red][red];[/red]
  [black][b]my[/b][/black] [red]([/red][blue]$key[/blue], [blue]$val[/blue][red])[/red] = [url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url][red]([/red][red]/[/red][purple][purple][b]\s[/b][/purple]*,[purple][b]\s[/b][/purple]*[/purple][red]/[/red], [blue]$line[/blue][red])[/red][red];[/red]
  [blue]$subst_hash[/blue][red]{[/red][blue]$key[/blue][red]}[/red] = [blue]$val[/blue][red];[/red]
[red]}[/red]
[url=http://perldoc.perl.org/functions/close.html][black][b]close[/b][/black][/url][red]([/red]FILE2[red])[/red][red];[/red]
[black][b]print[/b][/black] [red]"[/red][purple]Done.[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

[gray][i]# writing $file3 from $file1 using %subst_hash[/i][/gray]
[black][b]print[/b][/black] [red]"[/red][purple]2. Now reading lines from '[blue]$file1[/blue]' and writing to '[blue]$file3[/blue]'...[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[black][b]open[/b][/black][red]([/red]FILE1, [blue]$file1[/blue][red])[/red]
  or [black][b]die[/b][/black] [red]'[/red][purple]Could not open $file1 for reading[/purple][red]'[/red][red];[/red]
[black][b]open[/b][/black][red]([/red]FILE3, [red]"[/red][purple]>[blue]$file3[/blue][/purple][red]"[/red][red])[/red]
  or [black][b]die[/b][/black] [red]'[/red][purple]Could not open $file3 for writing[/purple][red]'[/red][red];[/red]

[olive][b]while[/b][/olive] [red]([/red] [black][b]my[/b][/black] [blue]$line[/blue] = <FILE1> [red])[/red] [red]{[/red]
  [black][b]chomp[/b][/black][red]([/red][blue]$line[/blue][red])[/red][red];[/red]
  [olive][b]if[/b][/olive] [red]([/red][blue]$subst_hash[/blue][red]{[/red][blue]$line[/blue][red]}[/red][red])[/red] [red]{[/red]
     [black][b]print[/b][/black] FILE3 [red]"[/red][purple][blue]$subst_hash[/blue]{[blue]$line[/blue]}[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
  [red]}[/red]
[red]}[/red]

[black][b]close[/b][/black][red]([/red]FILE1[red])[/red][red];[/red]
[black][b]close[/b][/black][red]([/red]FILE3[red])[/red][red];[/red]
[black][b]print[/b][/black] [red]"[/red][purple]All Done.[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
[/tt]
Running:
Code:
C:\Users\Roman\Work>perl czarj.pl
1. Creating substitution hash from 'file2.txt'...
Done.
2. Now reading lines from 'file1.txt' and writing to 'file3.txt'...
All Done.
Result:
file3.txt
Code:
joe
bob
pawan
kimber
sue-lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top