Git Product home page Git Product logo

forline's Introduction

forline

A small utility to execute python instructions for each line of text files. Inspired by awk and the perl -n option missing in python. Based on http://code.activestate.com/recipes/437932/ with additional options for heading code (-b, which can be used for imports), trailing code (-e) and separators (-s). As in the original script, the variables line, words[] and num have the following meaning:

  • line refers to the current line (without carriage return),
  • words[n] refers to the nth word (starting from 0). The line is splitted with spaces or the separator given with the -s option
  • num is the current line number (starting from 1)

The first example emulates head -10:

$ cat exemple | forline "if num <= 10: print(line)"

Several instructions may be given in the body, either by using the semicolon separator or by using several body arguments:

$ cat exemple | forline "if num <= 10: print(num); print(line)"
$ cat exemple | forline "if num <= 10: print(num)" "print(line)"

Printing then testing to exit the loop must be written using two strings. Python syntax does not accept a simple statement followed by a compound statement in the same line. Refer to the definition of statements at https://docs.python.org/2/reference/compound_stmts.html. This is the only difficulty to keep in mind when using forline.

$ cat exemple | forline "print(line)" "if num == 10: break"

Emulating tail -10 illustrates the use of heading and trailing code sections.

$ cat exemple | forline -b "x = []" -e "for _ in x: print(_)" "x = (x + [line])[-10:]"

The next example removes duplicated lines.

$ cat exemple | forline -b "lines = set()" "if line not in lines: lines.add(line); print(line)"

The next example sums the first word matching an integer in each line.

$ cat exemple | forline -b "s = 0" -e "print(s)" "m = re.search(r'\b(\d+)\b', line)" "if m: s += int(m.group(1))"

Note re module is always imported (i.e. import re in starting argument is implicit).

Installation

Download the zip file, unzip and and key pip install . in forline directory.

Syntax, error and behaviour

forline uses python 3 and does not support python 2. An additional -t option enables to trace the generated script. Using the generated script is strictly equivalent than using forline command line. As a consequence, all questions regarding syntax or execution errors may be answered by running the generated script.

forline's People

Contributors

gillesarcas avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.