|
|
Subject:
Perl script
Category: Computers > Programming Asked by: johnnymurph-ga List Price: $150.00 |
Posted:
02 Aug 2006 06:01 PDT
Expires: 01 Sep 2006 06:01 PDT Question ID: 751826 |
i need a perl script that will read two text files and write to one of them. in the first file are four coloums, the last two contain an english word and beside it is its spanish transalation. the second file contains six coloums one of which is a list of english words. I need a scipt that will read each line of the first file, checking to see it the english word in file one is contained in file 2, if this is the case then I need the spanish word to replace the english word in the second file. |
|
There is no answer at this time. |
|
Subject:
Re: Perl script
From: feldersoft-ga on 02 Aug 2006 15:00 PDT |
I'll write the script for you for the listed fee if no one answers. Comment if you're interested, I'll be checking back periodically. |
Subject:
Re: Perl script
From: bobatpurdue-ga on 02 Aug 2006 19:05 PDT |
Which column in the second file contains the English words? |
Subject:
Here is the code
From: bobatpurdue-ga on 02 Aug 2006 19:52 PDT |
#!/usr/bin/perl ## Reads in the 1st file that is tab delimited and then makes subsitutions ## in the second file. ## 1st file name $fileone="1.txt"; ## 2nd file $filetwo="2.txt"; ## Column number in the 2nd file from 1 to 6 that is the column to be replaced $column_english = 5; ## Open the 1xt file to read in the English and Spanish words open (FILEIN, $fileone) or die "Can't open first file.\n";; ## Read in each line and create a hash table while ($linein = <FILEIN>) { chomp $linein; @arrayin = split(/\t/, $linein); $englishword = $arrayin[2]; $spanishword = $arrayin[3]; print "$englishword -> $spanishword\n"; $langhash{$englishword} = $spanishword; } close(FINEIN); ## Open second file to convert from English to Spanish open (FILEIN, $filetwo) or die "Can't open second file.\n";; ## Go through each line and replace while ($linein = <FILEIN>) { chomp $linein; @arrayin = split(/\t/, $linein); $englishword = $arrayin[$column_english-1]; ## If the word exists in the hash then replace if (exists $langhash{$englishword}) { for($i=0;$i<6;$i++) { if ($column_english==($i+1)) { print $langhash{$englishword}; } else { print $arrayin[$i]; } print "\t" if ($i <= 4); } print "\n"; } else { print "$linein\n"; } } close(FINEIN); |
Subject:
Re: Perl script
From: bobatpurdue-ga on 02 Aug 2006 19:53 PDT |
If you need it output to a file, let me know, it's an easy fix. |
Subject:
Re: Perl script From: bobatpurdue-ga on 02 Aug 2006 19:05 PDT
From: johnnymurph-ga on 03 Aug 2006 07:40 PDT |
Unfortunately that is not working. It is reading both files but it is not making the replacements. It is possible my files are different from what you are thinking of so below is a sample of each File1 id ref english_column spanish_column 22456 a above arriba 22457 d back revés 22458 d behind tras 22459 e below abajo File2 key_2 number pref fhgj english_word tag_count 987 1 q 1 above 0 988 2 w 1 back 0 965 3 a 1 back 0 654 1 z 1 back 0 |
Subject:
Re: Perl script From: bobatpurdue-ga on 02 Aug 2006 19:05 PDT
From: johnnymurph-ga on 03 Aug 2006 07:40 PDT |
Unfortunately that is not working. It is reading both files but it is not making the replacements. It is possible my files are different from what you are thinking of so below is a sample of each File1 id ref english_column spanish_column 22456 a above arriba 22457 d back revés 22458 d behind tras 22459 e below abajo File2 key_2 number pref fhgj english_word dvr 987 1 q 1 above 0 988 2 w 1 back 0 965 3 a 1 back 0 654 1 z 1 back 0 |
Subject:
Re: Perl script From: bobatpurdue-ga on 02 Aug 2006 19:05 PDT
From: johnnymurph-ga on 03 Aug 2006 08:05 PDT |
Have figured out the problem. If you can just add a fix so that it outputs to a new file I would be happy to accept this as the answer thanks JM |
Subject:
Re: Perl script
From: bobatpurdue-ga on 03 Aug 2006 09:44 PDT |
#!/usr/bin/perl ## Reads in the 1st file that is tab delimited and then makes subsitutions ## in the second file. ## 1st file name $fileone="1.txt"; ## 2nd file $filetwo="2.txt"; ## output file $fileout="out.txt"; ## Column number in the 2nd file from 1 to 6 that is the column to be replaced $column_english = 5; ## Open the 1xt file to read in the English and Spanish words open (FILEIN, $fileone) or die "Can't open first file.\n";; ## Read in each line and create a hash table while ($linein = <FILEIN>) { chomp $linein; @arrayin = split(/\t/, $linein); $englishword = $arrayin[2]; $spanishword = $arrayin[3]; print "$englishword -> $spanishword\n"; $langhash{$englishword} = $spanishword; } close(FINEIN); ## Open second file to convert from English to Spanish open (FILEIN, $filetwo) or die "Can't open second file.\n";; open (FILEOUT, ">$fileout") or die "Can't open output file.\n";; ## Go through each line and replace while ($linein = <FILEIN>) { chomp $linein; @arrayin = split(/\t/, $linein); $englishword = $arrayin[$column_english-1]; ## If the word exists in the hash then replace if (exists $langhash{$englishword}) { for($i=0;$i<6;$i++) { if ($column_english==($i+1)) { printf FILEOUT $langhash{$englishword}; } else { printf FILEOUT $arrayin[$i]; } printf FILEOUT "\t" if ($i <= 4); } printf FILEOUT "\n"; } else { printf FILEOUT "$linein\n"; } } close(FINEIN); close(FINEOUT); |
Subject:
Re: Perl script
From: bobatpurdue-ga on 03 Aug 2006 09:45 PDT |
Does it handle the spanish unique characters correctly? |
Subject:
Re: Perl scripts - bobatpurdue-ga on 03 Aug 2006 09:45 PDT
From: johnnymurph-ga on 03 Aug 2006 10:03 PDT |
Thanks for that - just one problem the out file does not contain the changes, the changes are appearing on the screen but the out file is just a duplication of file2. whereas I need the changes actually in the file, i.e. I need the spanish word to replace the english word in the out file. Is this possible? |
Subject:
Re: Perl script
From: bobatpurdue-ga on 03 Aug 2006 10:56 PDT |
Yes. However I'm not getting the same type of response. See below: [manningr@fileserve ~]$ cat 1.txt d ref english_column spanish_column 22456 a above arriba 22457 d back reves 22458 d behind tras 22459 e below abajo [manningr@fileserve ~]$ cat 2.txt key_2 number pref fhgj english_word tag_count 987 1 q 1 above 0 988 2 w 1 back 0 965 3 a 1 back 0 654 1 z 1 back 0 [manningr@fileserve ~]$ cat out.txt key_2 number pref fhgj english_word tag_count 987 1 q 1 arriba 0 988 2 w 1 reves 0 965 3 a 1 reves 0 654 1 z 1 reves 0 [manningr@fileserve ~]$ Do you have tabs seperating each column or something else? This would cause the problem. There is a second easier option. We could just replace every english word in the file with the spanish word regardless of the column it is in. Your thoughts? |
Subject:
Re: Perl script
From: johnnymurph-ga on 03 Aug 2006 11:07 PDT |
yes, i think then replace every english word in the file with the spanish word regardless of the column it is in. |
Subject:
Re: Perl script
From: bobatpurdue-ga on 03 Aug 2006 12:22 PDT |
Two more questions. Do you have admin or root access to the box? Secondly approximately how many lines in the second file are there? (10, 1000, million, etc..) |
Subject:
Re: Perl script
From: johnnymurph-ga on 03 Aug 2006 12:36 PDT |
Hi, I dont think its an access problem though I still dont know why the file is working for you and not for me because the way it works for you is ideal for my purposes. also both files are tab separated and appear exactly as the sample shown. There are over 1 million lines in the second file.. thanks for all the help JM |
Subject:
Re: Perl script bobatpurdue-ga
From: johnnymurph-ga on 04 Aug 2006 04:00 PDT |
Thats great it works, thats answered my question |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |