I am getting an unexpected output from a one-liner and I do not understand what it is I am doing wrong. the one-liner is outputting a "1" before the line number. I've tried to simplify the one-liner to try to understand the output but still eludes me but, maybe, someone here can part with some of the wisdom.

____data.txt____

name = Leah last_name = Pellegrini age = 32 profession = "artist" / "e +ngineer of all things" favorite_song = "I'm alive" performer = "Micha +el Franti" name = alberto last_name = Montelongo age = 32 profession = "cool + dude" favorite_song = "#41" performeer = "DMB" Name = Lucca last_name = montelongo age = 6 profession = "future Blue +Angel" favorite-song = "sunflower" performer = "Post Malone" Name = josh last_name = montelongo age = 26 profess +ion = "happy" favorite-song = "satellite" performer = "dmb" Name = josh last_name = montelongo age = 26 profession = "happy" favo +rite-song = "satellite" performer = "dmb"

1) One-liner seeking to build up to do multiple things like only print odd lines, print lines with text only, OR print if regexp match. Then print line with no leading space, number of the lines, space out with a \tab.

 perl -lane 'print s/^\s+//,$., "\t", @F[0,1,2,],"\t",@F[3,4,5], if (($.%2) && /\w+/ || /cool dude/)' data.txt
13 name=alberto last_name=Monte 5 Name=Lucca last_name=monte

Please, notice the 1 before the odd line number 3, Why is that? Is my substitution not written correctly? I am replacing the space with no space. If I remove this substitution the output is fine but it is unexpected to me and I cannot understand the mechanism that is at work here with this substitution.

2) Now, the same one-liner but printing the even lines.

perl -lane 'print s/^\s+//,$., "\t", @F[0,1,2,],"\t",@F[3,4,5], if (($.%2==0) && /\w+/ || /cool dude/)' data.txt
2 name=Marta last_name=Pellegrini 13 name=alberto last_name=Monte 16 Name=josh last_name=monte 8 Name=josh last_name=monte

This is to illustrate the susbtitution in line 3 (satisfies OR condition) and 6(even lines). I'll appreciate and explanation why the substitution is outputing these ones.

Additional challenge: count the repeating names in the data.txt. I did a ~tr/=// as a character count but unable to think of a counter for "name" to add to this one-liner ;-)


In reply to One liner is returning unexpected "1" on the output and I do not understand the mechanism at play. by perlynewby

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.