Hi, I have a perl script that read lines from one file, add a string to it, and compares this with a second file. The printout should state if it is found or not.
The file utrancell_edited2.txt has format like this:
...
4215
4216
4224
4225
4234
4235
4244
4245
4254
4255
4256
4264
4265
4266
4274
4275
4284
...
The file femto_check_edited2.txt has format like this:
...
4244-Femto_10
4244-Femto_11
4244-Femto_12
4244-Femto_13
4244-Femto_14
4244-Femto_15
4245-Femto_10
4245-Femto_11
4245-Femto_12
4245-Femto_13
4245-Femto_14
4245-Femto_15
4254-Femto_10
4254-Femto_11
4254-Femto_12
4254-Femto_13
4254-Femto_14
4254-Femto_15
4255-Femto_10
4255-Femto_11
4255-Femto_12
4255-Femto_13
4255-Femto_14
4255-Femto_15
4256-Femto_10
4256-Femto_11
...
The script adds "-Femto_xx" to the line from utrancell_edited2.txt and generates a third file "kids.txt" containing the utrancells not found in femto_check_edited2.txt. The problem is that the grep function does not seem to work properly. I get a result where it says:
...
4245-Femto_14 not defined
4245-Femto_15 not defined
4254-Femto_10 not defined
4254-Femto_11 not defined
4254-Femto_12 not defined
4254-Femto_13 not defined
4254-Femto_14 not defined
4254-Femto_15 not defined
4255-Femto_10 not defined
4255-Femto_11 not defined
4255-Femto_12 not defined
4255-Femto_13 not defined
4255-Femto_14 not defined
4255-Femto_15 not defined
...
However if I check the file, all the strings mentioned above are in the file. Why doesn't the grep function recognize it?
I am quite new to perl scripting so I am sure I don't do this the most effective way, but its working for some, so why not for all?
The script I am using is this:
#!/usr/bin/perl system ("rm /tmp/kids.txt"); open INPUT, "/tmp/femto_check_edited2.txt" or die $!; open (IN,"/tmp/utrancell_edited2.txt"); $kids = "/tmp/kids.txt"; open my $kids, ">", "/tmp/kids.txt" or die "Could not open kids.txt: $ +!"; #overwrite the file for clearing while(<INPUT>){ chomp($_); push @list_match, $_; }#while close INPUT; while ($line=<IN>) { chop ($line); $result10 = grep(/$line-Femto_10/,@list_match); $result11 = grep(/$line-Femto_11/,@list_match); $result12 = grep(/$line-Femto_12/,@list_match); $result13 = grep(/$line-Femto_13/,@list_match); $result14 = grep(/$line-Femto_14/,@list_match); $result15 = grep(/$line-Femto_15/,@list_match); if ($result10 eq 1){ print "$line-Femto_10\n"; }else{ print "$line-Femto_10 er ikke definert\n"; my $kids = "/tmp/kids.txt"; open (OUTFILE, ">>$kids"); print OUTFILE <<"naken"; #unique end of script $line-Femto_10 er ikke definert naken close OUTFILE; }#if if ($result11 == 1){ print "$line-Femto_11\n"; }else{ print "$line-Femto_11 er ikke definert\n"; my $kids = "/tmp/kids.txt"; open (OUTFILE, ">>$kids"); print OUTFILE <<"naken"; #unique end of script $line-Femto_11 er ikke definert naken close OUTFILE; }#if if ($result12 == 1){ print "$line-Femto_12\n"; }else{ print "$line-Femto_12 er ikke definert\n"; my $kids = "/tmp/kids.txt"; open (OUTFILE, ">>$kids"); print OUTFILE <<"naken"; #unique end of script $line-Femto_12 er ikke definert naken close OUTFILE; }#if if ($result13 == 1){ print "$line-Femto_13\n"; }else{ print "$line-Femto_13 er ikke definert\n"; my $kids = "/tmp/kids.txt"; open (OUTFILE, ">>$kids"); print OUTFILE <<"naken"; #unique end of script $line-Femto_13 er ikke definert naken close OUTFILE; }#if if ($result14 == 1){ print "$line-Femto_14\n"; }else{ print "$line-Femto_14 er ikke definert\n"; my $kids = "/tmp/kids.txt"; open (OUTFILE, ">>$kids"); print OUTFILE <<"naken"; #unique end of script $line-Femto_14 er ikke definert naken close OUTFILE; }#if if ($result15 == 1){ print "$line-Femto_15\n"; }else{ print "$line-Femto_15 er ikke definert\n"; my $kids = "/tmp/kids.txt"; open (OUTFILE, ">>$kids"); print OUTFILE <<"naken"; #unique end of script $line-Femto_15 er ikke definert naken close OUTFILE; }#if }#while close IN;
In reply to grep function does not recognize string by korak
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |