http://qs321.pair.com?node_id=1147450

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

I have an 8-column input tab-delimited .txt file and wish to print the contents of column 3 + 8 when the value held within column 8 is >0.

I've tried:

perl -lane 'print "$F[2]\t$F[7]" if $F[7] > 0' < input.txt

However this only returns a single line (the first line) when there are multiple matching lines. I want it to iterate over every line instead. What have I done wrong?

For example, if my input file contains:

77    *    10    0    *    *    0    340

141    *    40    0    *    *    0    240

The output file will only contain:

10    340

Replies are listed 'Best First'.
Re: One-liner only checking the first line of the input file
by stevieb (Canon) on Nov 11, 2015 at 14:23 UTC

    Are your line endings what you expect (ie. is the file being treated as one long line as opposed to several)? Try this, and see if you get a single line, or all of the lines printed:

    perl -lane 'print $_; exit;' input.txt
      perl -lane 'print $_; exit;' input.txt > output.txt

      Output.txt appears identical to the input file - all lines are printed.

        Do this:

        perl -anE '/(\R)/; say unpack("H*", $1);' input.txt

        It appears as your file is being read as one long string. If you're on *nix, your output should be 0a per line for as many lines you have in the file. Post what the output shows.

Re: One-liner only checking the first line of the input file
by GotToBTru (Prior) on Nov 11, 2015 at 14:44 UTC

    I copied your code and data verbatim and it works for me.

    howard$: cat input.txt 77 * 10 0 * * 0 340 141 * 40 0 * * 0 240 howard$: perl -lane 'print "$F[2]\t$F[7]" if $F[7] > 0' < input.txt 10 340 40 240

    Environment is AIX, Perl 5.8.8.

    Dum Spiro Spero
Re: One-liner only checking the first line of the input file
by Laurent_R (Canon) on Nov 11, 2015 at 15:07 UTC
    Since your code and data seem to be working under Unix, are you perhaps working under Windows (or old Mac) with a file prepared with an Unix format?

    Under which OS are your working?

      OS X (10.11.2)

      This is perl 5, version 18, subversion 2 (v5.18.2) built for darwin-thread-multi-2level (with 2 registered patches, see perl -V for more detail)

      File was prepared on the same system.

Re: One-liner only checking the first line of the input file
by hippo (Bishop) on Nov 11, 2015 at 14:39 UTC

    Your code and data as given work for me. The output (on perl v5.20.3) is:

    10 340 40 240

    Are you sure your data set is what you think it is? Does adding -w to the options do you any good?

      Including -w produces the same result.

      I've uploaded a small sample of my .txt file here:

      https://www.dropbox.com/s/jelbmyf7nzn3n70/READS.txt?dl=0

        Thanks for that. I don't use dropbox so have no concept of how (or not) they might have munged that. Instead, here are some hashes of my input.txt which works:

        • SHA256: e49d0b5631cfdf9de7bc4571716e368c5fbfb394d9f224caa7c63c7608c55b77
        • SHA1: 343eca42bb1e78dbf69eb3c2736afb65ab680dce
        • MD5: afe5fb04f8d6edcf57a04eb3ced196ef

        Compare one of those with the hash of your source and see if/how it differs. I'm using unix EOLs and am including the blank line in the middle.