Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Groovy 2 Cookbook

You're reading from   Groovy 2 Cookbook Java and Groovy go together like ham and eggs, and this book is a great opportunity to learn how to exploit Groovy 2 to the full. Packed with recipes, both intermediate and advanced, it's a great way to speed up and modernize your programming.

Arrow left icon
Product type Paperback
Published in Oct 2013
Publisher Packt
ISBN-13 9781849519366
Length 394 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Luciano Fiandesio Luciano Fiandesio
Author Profile Icon Luciano Fiandesio
Luciano Fiandesio
Andrey Adamovich Andrey Adamovich
Author Profile Icon Andrey Adamovich
Andrey Adamovich
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Groovy 2 Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
1. Getting Started with Groovy 2. Using Groovy Ecosystem FREE CHAPTER 3. Using Groovy Language Features 4. Working with Files in Groovy 5. Working with XML in Groovy 6. Working with JSON in Groovy 7. Working with Databases in Groovy 8. Working with Web Services in Groovy 9. Metaprogramming and DSLs in Groovy 10. Concurrent Programming in Groovy Index

Using Groovy as a command-line text file editor


The groovy command, which we introduced in the Executing Groovy code from the command line recipe, can also be used as a stream editor or text file filter. In this recipe, we will cover the -i, -n, and -p parameters that can be used to leverage file editing and processing functionality.

How to do it...

Assume that you have a file, data.txt, which contains five lines with numbers from 1 to 5:

  1. To multiply each number by 2, you can use the following command:

    groovy -n -e "println line.toLong() * 2" data.txt
    
  2. We can even omit the println method call if we pass additional the -p parameter to the command:

    groovy -n -p -e "line.toLong() * 2" data.txt
    
  3. In both cases, Groovy will print the following output:

    2
    4
    6
    8
    10
    

How it works...

Due to the fact that we are using the -n option, the code in double quotes is applied to each line read from the datafile specified as the last parameter in the command line. The line variable is predefined by Groovy, and you can use it to access, filter, or modify the line's content.

There's more...

If you add the -i option to the previous command, then it will actually modify the input file, with output values as follows:

groovy -i -n -p -e "line.toLong() * 2" data.txt

Adding a suffix .bak to the -i option will save the original input file data.txt under data.txt.bak:

groovy -i .bak -n -p -e "line.toLong() * 2" data.txt

You can use the -n and -p options to filter the input stream of other operating system commands. For example, if you want to filter the output of a directory listing command (dir) to show only the *.jar files, on Windows you can use the following command:

dir | groovy -n -e "if (line.contains('.jar')) println line"

Or on *nix-based operating systems, you can use the following command:

ls -la | groovy -n -e "if (line.contains('.jar')) println line"

Of course, the result of the previous commands can be easily achieved by more efficient operating system instructions. However, these examples are given to demonstrate that you can actually leverage the full power of the Groovy and Java programming languages to implement more complex processing rules.

See also

  • Executing Groovy code from the command line

  • Using Groovy to start a server on the command line

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime