gothamcity has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a text file that has several columns of data that are tab separated. I'm trying to use command-line perl to read the file, make a substitution, and write the modified version. However, when I do so, the output file is blank.

Here is my command:
perl -i.bak -F\t -lane "$F[4] =~ s/.*y.*/yes/" results.txt
Here's a segment of my data (results.txt):
name1 name2 NA NA ye[s]* no OK name1 name2 NA NA <ls>yes no OK name1 name2 NA NA <ls>yes<ln> no OK
Basically, I'm trying to clean up the fifth column so that it is only 'yes' without extra information.

Any help is greatly appreciated.

Replies are listed 'Best First'.
Re: In-place edit on tabular data not working
by Corion (Patriarch) on Oct 18, 2010 at 18:05 UTC

    See perlrun on -n. If you use the -n switch, you need to do the printing yourself. Did you maybe want the -p switch?

      Thanks, I got it to print out using -p, but the substitution is not being printed in the output (parsed) file.

      I know that the regex is working because when i run the following it prints the substituted words.
      perl -F\t -lape "$F[4] =~ s/.*y.*/yes/; print $F[4], \"\n\";" results. +txt
      Not sure why the substituted words aren't being written into the file. Any thoughts? Thanks!
Re: In-place edit on tabular data not working
by toolic (Bishop) on Oct 18, 2010 at 18:19 UTC
    All you did was change one element of the @F array. You did not change $_. You need to reconstruct $_, and as Corion said, use -p instead of -n:
    perl -i.bak -lape '$F[4] =~ s/.*y.*/yes/; $_ = join "\t", @F' results. +txt

    -p says to use print, which prints $_ by default.

      Thank you so much! Reconstructing the $_ for worked. I see the mistake I was making: the modified $F4 wasn't making it back to $_ for output.