![]() ![]() It's basically short for, grep for the line is 0. In this particular one-liner we use int($.) ~~ (13, 19, 67) that determines if numeric value $. ![]() You can do all kinds of smart matching with it, for example, check if two arrays are the same, if an array contains an element, and many other use cases (see perldoc perlsyn). The smart matching operator ~~ appeared only in Perl 5.10. If you have Perl 5.10 or later then you can use the ~~ smart match operator, Just like in previous one-liner, we check if the current line is line 27, if it's not then we print it, otherwise we skip it.Īnother way to write the same is to reverse print and $. has value 13, then we print the line and exit. special variable stands for "current line number". It checks if the length of a line is less than 80 characters.Īs I explained in one-liner #83, the $. This is the opposite of previous one-liner. Print lines that are less than 80 chars in length. In fact, length, length() and length($_) are the same.ĩ4. In this one we omitted brackets for length function call. In Perl you can sometimes omit the brackets () for function calls. This one-liner prints all lines that are 80 chars or longer. Print the first 10 lines of a file (emulate head -10). The first line got printed and that's all we wanted.Ĩ3. Here Perl reads in the first line into $ variable thanks to -n option, then it calls print statement that prints the contents of the $ variable. Print the first line of a file (emulate head -1). Or the other way around! For example, to delete all lines with even line numbers, print the odd lines, and to delete odd lines print the even lines.Īfter I am done with the 8 parts of the article, I will release the whole article series as a pdf e-book! Please subscribe to my blog to be the first to get it!Īwesome news: I have written an e-book based on this article series. If you want to delete certain lines, you just print the lines you're interested in. The selective printing and selective deleting of certain lines is actually the same process. Part IX: Release of Perl One-Liners e-book. ![]() Part VI: Selective printing and deleting of certain lines (this part).Part V: Text conversion and substitution.Part IV: String creation and array creation.The article on Perl one-liners will consist of nine parts: Perl one-liners is my attempt to create " perl1line.txt" that is similar to " awk1line.txt" and " sed1line.txt" that have been so popular among Awk and Sed programmers and Linux sysadmins. See part one for introduction of the series. In this part I will create various one-liners for selective printing and deleting of certain lines. This is the sixth part of a nine-part article on Perl one-liners. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |