Re: Replacing variable data between fields - Revisited.
Perlwannabe wrote: I originally tried to do this, but it won't work. The data doesn't _always_ have a tab before address, sometimes (although seldom) it has a space. With grep I would miss the entire address. However, if I were to just select everything and replace it with nothing, that would be the answer. BTW...your solution above is right on the money for about 98% of the file. However, by simply scanning the line and deleting everything between between HOW and NUMBER: that would give me 100%. Any ideas? You could try this: s/\tHOW.+?\sNUMBER:\s*\S+\t/\t/; Hah!!! I got it. FINALLY!!! Thank you so much for the help. It was a simple matter of using s/// instead of tr///. I am not sure why, but it works...The tr/// function does not work at all...Any ideas why? s{ (?=HOW) (.+?) (?=NUMBER\t) } { ( $a = $1 ) =~ s/$a/ /; $a }ex; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Replacing variable data between fields - Revisited.
Perlwannabe wrote: I posted a problem to the mailing list with a similar question I had some time ago regarding replacing data between fields. Unfortunately, I am having a difficult time with the problem. I am including the code and a sample of the data. Could someone please give me a clue as to why this is not working. I have exhausted myself before re-contacting the list. When I run the code, the file remains exactly the same and nothing is changed or replaced. BEGIN CODE ## #!/usr/bin/perl use warnings; use strict; use LWP::Simple; my $file10 = C:/Test_Folder/test_data/input.txt; #declare the input file as the static file name open(FILE,$file10) || die Could not open file for reading! $!;#open file for reading open(TEMP,$file10.tmp) || die Could not open file for writing! $!;#open file for writing while(FILE){ #begin while s{ (?=HOW) (.+?) (?=NUMBER:) } { ( $a = $1 ) =~ tr/\t/ /; $a }ex; print TEMP $_;#print result to temp file }#end while #Close the files. This should happen automatically, but the routine is written for safety close FILE || die Could not close file! $!; close TEMP || die Could not close file! $!; unlink $file10; #remove the old file rename($file10.tmp,$file10) || die The file could not be renamed! $!;#rename the temp file to the old file's name # END CODE # Here is a sample of the data file: Name:tabJohn SmithtabAddress: 1234 Sparrow Ave.tabHOW MANY CHILDREN: 3tabNUMBER OF PETS: 2tabTYPE OF PETS: Fish, DogtabFAVORITE NUMBER: 13tabCity: New YorktabState: New YorktabZip: 11011 Obviously, I do not want all of the useless information between Address and City. I want to delete everything between HOW and NUMBER: (including the HOW and NUMBER:tab) So my file will look like this: Name:tabJohn SmithtabAddress: 1234 Sparrow Ave.tabCity: New YorktabState: New YorktabZip: 11011 According to your data, this seems to work: while ( FILE ) { print TEMP join '', grep /(?:^|\t)(?i:name|address|city|state|zip):/, # fields to keep /(?:^|\t)[^:]+?:\s+[^\t]+/g; # split the fields } John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Replacing variable data between fields - Revisited.
Perlwannabe wrote: I posted a problem to the mailing list with a similar question I had some time ago regarding replacing data between fields. Unfortunately, I am having a difficult time with the problem. I am including the code and a sample of the data. Could someone please give me a clue as to why this is not working. I have exhausted myself before re-contacting the list. When I run the code, the file remains exactly the same and nothing is changed or replaced. BEGIN CODE ## #!/usr/bin/perl use warnings; use strict; use LWP::Simple; my $file10 = C:/Test_Folder/test_data/input.txt; #declare the input file as the static file name open(FILE,$file10) || die Could not open file for reading! $!;#open file for reading open(TEMP,$file10.tmp) || die Could not open file for writing! $!;#open file for writing while(FILE){ #begin while s{ (?=HOW) (.+?) (?=NUMBER:) } { ( $a = $1 ) =~ tr/\t/ /; $a }ex; print TEMP $_;#print result to temp file }#end while #Close the files. This should happen automatically, but the routine is written for safety close FILE || die Could not close file! $!; close TEMP || die Could not close file! $!; unlink $file10; #remove the old file rename($file10.tmp,$file10) || die The file could not be renamed! $!;#rename the temp file to the old file's name # END CODE # Here is a sample of the data file: Name:tabJohn SmithtabAddress: 1234 Sparrow Ave.tabHOW MANY CHILDREN: 3tabNUMBER OF PETS: 2tabTYPE OF PETS: Fish, DogtabFAVORITE NUMBER: 13tabCity: New YorktabState: New YorktabZip: 11011 Obviously, I do not want all of the useless information between Address and City. I want to delete everything between HOW and NUMBER: (including the HOW and NUMBER:tab) So my file will look like this: Name:tabJohn SmithtabAddress: 1234 Sparrow Ave.tabCity: New YorktabState: New YorktabZip: 11011 According to your data, this seems to work: while ( FILE ) { print TEMP join '', grep /(?:^|\t)(?i:name|address|city|state|zip):/, # fields to keep /(?:^|\t)[^:]+?:\s+[^\t]+/g; # split the fields } I originally tried to do this, but it won't work. The data doesn't _always_ have a tab before address, sometimes (although seldom) it has a space. With grep I would miss the entire address. However, if I were to just select everything and replace it with nothing, that would be the answer. BTW...your solution above is right on the money for about 98% of the file. However, by simply scanning the line and deleting everything between between HOW and NUMBER: that would give me 100%. Any ideas? BTW...can you see why the original code I posted is not working? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Replacing variable data between fields - Revisited.
Perlwannabe wrote: I originally tried to do this, but it won't work. The data doesn't _always_ have a tab before address, sometimes (although seldom) it has a space. With grep I would miss the entire address. However, if I were to just select everything and replace it with nothing, that would be the answer. BTW...your solution above is right on the money for about 98% of the file. However, by simply scanning the line and deleting everything between between HOW and NUMBER: that would give me 100%. Any ideas? You could try this: s/\tHOW.+?\sNUMBER:\s*\S+\t/\t/; John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Replacing variable data between fields - Revisited.
I posted a problem to the mailing list with a similar question I had some time ago regarding replacing data between fields. Unfortunately, I am having a difficult time with the problem. I am including the code and a sample of the data. Could someone please give me a clue as to why this is not working. I have exhausted myself before re-contacting the list. When I run the code, the file remains exactly the same and nothing is changed or replaced. BEGIN CODE ## #!/usr/bin/perl use warnings; use strict; use LWP::Simple; my $file10 = C:/Test_Folder/test_data/input.txt; #declare the input file as the static file name open(FILE,$file10) || die Could not open file for reading! $!;#open file for reading open(TEMP,$file10.tmp) || die Could not open file for writing! $!;#open file for writing while(FILE){ #begin while s{ (?=HOW) (.+?) (?=NUMBER:) } { ( $a = $1 ) =~ tr/\t/ /; $a }ex; print TEMP $_;#print result to temp file }#end while #Close the files. This should happen automatically, but the routine is written for safety close FILE || die Could not close file! $!; close TEMP || die Could not close file! $!; unlink $file10; #remove the old file rename($file10.tmp,$file10) || die The file could not be renamed! $!;#rename the temp file to the old file's name # END CODE # Here is a sample of the data file: Name:tabJohn SmithtabAddress: 1234 Sparrow Ave.tabHOW MANY CHILDREN: 3tabNUMBER OF PETS: 2tabTYPE OF PETS: Fish, DogtabFAVORITE NUMBER: 13tabCity: New YorktabState: New YorktabZip: 11011 Obviously, I do not want all of the useless information between Address and City. I want to delete everything between HOW and NUMBER: (including the HOW and NUMBER:tab) So my file will look like this: Name:tabJohn SmithtabAddress: 1234 Sparrow Ave.tabCity: New YorktabState: New YorktabZip: 11011 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]