Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Don't ask to ask, just ask

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
A year and a half into mucking around with the -nep flags I still get confused or flustered when I cook up one liners (often basic search/replace one liners), especially because I often find myself switching from unix to dos. Going through the following exercises on my (unix) console helped reduce my confusion quite a bit, and I think doing this earlier on would have helped me, so I'm posting to here, hoping to help newer monks who experience brain melt reading perlrun. Since the audience is people new to perl, I also included some examples of basic regex substitution from the command line, since I think this is something a lot of people want to do early on.
linux4:/home/thomas/learning # perl -e No code specified for -e. linux4:/home/thomas/learning # perl -e '#execute um' linux4:/home/thomas/learning # linux4:/home/thomas/learning # perl -e 'print "hello world" #execute u +m' hello worldlinux4:/home/thomas/learning # hello worldlinux4:/home/thomas/learning # perl -e 'print "hello world\ +n"' hello world linux4:/home/thomas/learning # linux4:/home/thomas/learning # perl -e "print qq(hello world\n) #same, + but dos compatible quoting (dos no like um single quotes)" hello world linux4:/home/thomas/learning # perl -ne '#read um from the console and + execute um (but nothing to execute here)' a << Type this into the console and hit return b c << End the console input with ctrl-d!) linux4:/home/thomas/learning # perl -pe '#read um and print um' a << This gets typed into the console a << This gets printed out b b c c linux4:/home/thomas/learning # perl -pe '$_ = "a\n" #read um, reset $_ +, and print um' a a b a c a linux4:/home/thomas/learning # perl -ne 'print $_ #read um and print u +m by executing um' a a b b c c linux4:/home/thomas/learning # perl -ne 'print #read um and print um b +y executing um with the default variable to print' a a b b c c linux4:/home/thomas/learning # perl -pe 's/b/a/ #substitute um, just u +m first b' aa aa bb ab cc cc linux4:/home/thomas/learning # perl -pe '$_ =~ s/b/a/ #Same as the fir +st s/// example but more verbose, explicitly giving the default argum +ents to s///' aa aa bb ab cc cc linux4:/home/thomas/learning # perl -ne 's/b/a/; print #Same thing usi +ng -n instead of -p' aa aa bb ab cc cc linux4:/home/thomas/learning # perl -ne '$_ =~ s/b/a/; print $_ #Same +thing more verbosely' aa aa bb ab cc cc linux4:/home/thomas/learning # perl -pe 's/b/a/g #substitute um, all u +m bs' aa aa bb aa cc cc linux4:/home/thomas/learning # perl -pe 's/./a/g #substitute um, all u +m, all um um!' aa aa bb aa cc aa all um um aaaaaaaaa linux4:/home/thomas/learning # perl -ane 'print "$F[0]$F[4]\n" #autosp +lit mode, print the first and fifth fields (zero-indexed)' a b c d e ae f g h j i j k l m fi linux4:/home/thomas/learning # perl -ape '$_ = "$F[0]$F[4]\n" #same t +hing using -p' a b c d e ae linux4:/home/thomas/learning # perl -ane 'print "$G[0]$G[4]\n" #no mag +ic unless you name the array @F' a b c d e linux4:/home/thomas/learning # perl -ape '$_ = join (" ", @F)' a b c d e a b c d e
This is obviously a very basic intro that doesn't cover a lot of the possibilities, but I hope it will be of help. Monks seeking more enlightenment are encouraged to visit the award winning Perl White Magic - Special Variables and Command Line Switches. Also recommended is What one-liners do people actually use?, which inspired me to write this in the first place.

Ideas on how to improve / expend this post are welcome and will be integrated.

In reply to A gentle introduction/tutorial to the perl command line flags. by tphyahoo

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (8)
    As of 2014-04-18 08:16 GMT
    Find Nodes?
      Voting Booth?

      April first is:

      Results (464 votes), past polls