/r/programming is a reddit for discussion and news about computer programming. Guidelines. Please keep submissions on topic and of high. I wrote an ebook 2 months ago on Awk called "Awk One-Liners Explained." http:// It teaches Awk through. Here is the Awk One-Liners Explained article: Part One, Part Two, Part My future plans are to publish a free ebook with all the one-liners.

Awk One-liners Explained Ebook

Language:English, Japanese, Portuguese
Published (Last):21.07.2015
ePub File Size:29.62 MB
PDF File Size:9.58 MB
Distribution:Free* [*Sign up for free]
Uploaded by: VICTORIA

This is a preview copy of my first ever e-book. The e-book is called "Awk One- Liners Explained" and it explains 70 short awk programs for doing various text. This is my second e-book called "Sed One-Liners Explained". The ebook explains 27 Join pairs of lines side-by-side (emulates "paste" Unix command). Print UNIX time (seconds since Jan 1, , UTC). Print . Thanks! Thank you for downloading my "Perl One-Liners Explained" e-book! This is.

I explain the -i, -p, and -e arguments in the e-book in great detail.

AWK One Liners

It uses the list range operator.. Then a random letter is chosen by rand 26 and this operation is repeated 8 times.

Example 4: Print all lines from line 17 to line 30 perl -ne 'print if Combining the flip-flop operator with print if makes it print only lines Example 5: Remove all consecutive blank lines, leaving just one perl pe0 I included this one-liner here in the examples just to show you how funny and obscure one-liners can get.

This one-liner deletes all repeated blank lines from the input or from the given file. It does it by enabling the paragraph slurp mode through command line argument, which reads the input paragraph-by-paragraph, rather than line-by-line, and prints the paragraphs.

This way any number of blank lines between the paragraphs get ignored. I explain this one-liner in more details in the e-book.

As I hope you can see, knowing how to write one-liners is very useful. It was one of my top priority tasks through the years to become very efficient in the shell. Literally every day when I'm programming, I have to do all kinds of data processing tasks, changing files, verifying output, doing quick calculations, parsing data, etc, and knowing Perl one-liners makes it really fast to get things done.

Now that I have written this e-book, you can become very efficient, too. Book Preview I prepared a free book preview that contains the first 13 pages of the book. It includes the table of contents, preface, introduction to Perl one-liners and the first page of the second chapter.

download it now! Tweet about my book!

Awk and Sed One-Liners Explained

Help me spread the word about my new book. I prepared a special link that you can use to tweet about it. What's next? I really love writing about programming and I have planned writing many more books. For example, a line "this is a test" splits in four pieces and NF gets set to 4. The empty line "" does not split into any pieces and NF gets set to 0. Using NF as a pattern can effectively filter out empty lines. This one liner says: "If there are any number of fields, print the whole line followed by newline.

Triple-space a file.

Numbering and Calculations 5. Number lines in each file separately. FNR variable contains the current line for each file separately.

For example, if this one-liner was called on two files, one containing 10 lines, and the other 12, it would number lines in the first file from 1 to 10, and then resume numbering from one for the second file and number lines in this file from 1 to FNR gets reset from file to file.

Number lines for all files together.

Preview copy (first 11 pages) Get full e-book at

It counts the input lines seen so far. Number lines in a fancy manner.

It takes format parameter just like a regular printf function. This one right-aligns line numbers, followed by a space and a colon, and the line.

Number only non-blank lines in files. This one-liner pre-increments variable 'a' each time the line is non-empty, then it appends the value of this variable to the beginning of line and prints it out.

Count lines in files emulates wc -l. It is executed when all the input has been exhausted. This one-liner outputs the value of NR special variable after all the input has been consumed. Print the sum of fields in every line. This one-liner loops over all fields in a line there are NF fields in a line , and adds the result in variable 's'.

Then it prints the result out and proceeds to the next line. Print the sum of fields in all lines. Notice how it did not initialize variable 's' to 0. It was not necessary as variables come into existence dynamically. It is necessary if there are no fields.

If there are no fields, "s" never comes into existence and is undefined. Printing an undefined value does not print anything i. Replace every field by its absolute value.

It loops over all fields in a line and checks if any of the fields is less than 0. If any of the fields is less than 0, then it just negates the field to make it positive. Fields can be addresses indirectly by a variable. Here is the same one-liner rewritten with curly braces for clarity.

Recent Posts

The 'print' statement gets executed after all the fields in the line have been replaced by their absolute values.Get your copy of the ebook at http: That being said, I admit this is far from being perfect since whitespace-only lines are not handled elegantly. At this moment sed has executed all the commands in this case just one.

Sed opens the le as the input stream and starts reading the data. You are on page 1of 12 Search inside document pkrumins Peteris Krumins peter catonmat.

ESTHER from Las Vegas
I love reading books oddly. Review my other articles. One of my extra-curricular activities is storm chasing.