Python and Java program that work as pipes

From dftwiki3
Revision as of 08:48, 18 February 2017 by Thiebaut (talk | contribs)
Jump to: navigation, search

--D. Thiebaut (talk) 08:45, 18 February 2017 (EST)


Gremlin.jpg

Pipes in Python


#! /usr/bin/env python
'''
gremlin.py
D. Thiebaut

This python program illustrates how to write a program
that will work equally well with information taken from
a file specified on the command line, or information
fed through stdin.

Typical use:

   chmod +x gremlin.py
   ./gremlin.py  someFileName.txt

   cat someFileName.txt | ./gremlin.py

'''

from __future__ import print_function
import sys

def main():
    # open the first argument on command line, or stdin if none is specified
    with open(sys.argv[1], 'r') if len(sys.argv) > 1 else sys.stdin as f:
        # read the whole contents and put it in memory
        lines = f.readlines()
        f.close()


    # filter each line and replace 'g' characters by "gremlin",
    # upper or lowercase.
    for line in lines:
        print( line.strip()
               .replace('g', 'gremlin' )
               .replace( 'G', 'Gremlin' ) )

main()


Pipes in Java


/*
  Gremlin.java
  D. Thiebaut
  
  A program illustrating how an app can work with both a file and with stdin as
  input.

  This program takes either a file given as argument, or stdin, and filters it, 
  replacing all 'g' characters by "gremlin" and all 'G' characters by "Gremlin"

  Typical use:
  javac Gremlin.java
  java Gremlin someFileName.txt
  cat someFileName.txt | java Gremlin
  
  Some code taken from https://www.caveofprogramming.com/java/java-file-reading-and-writing-files-in-java.html

 */
import java.io.*;
import java.util.Scanner;

class Gremlin {
    
    public static void main(String[] args) {
	// read from pipe
	if ( args.length == 0 ) {
	    Scanner sc = new Scanner(System.in);
	    //System.out.println("Printing the file passed in:");
	    while(sc.hasNextLine()) System.out.println(sc.nextLine());
	}

	// read from file specified as argument (reads only first of 
	// several arguments).
	else {
	    // The name of the file to open.
	    String fileName = args[0];

	    // This will reference one line at a time
	    String line = null;
	    
	    try {
		// FileReader reads text files in the default encoding.
		FileReader fileReader = new FileReader(fileName);
		
		// Always wrap FileReader in BufferedReader.
		BufferedReader bufferedReader = 
		    new BufferedReader(fileReader);
		
		while((line = bufferedReader.readLine()) != null) {
		    // filter away!
		    line = line.replace( "g", "gremlin" ).replace( "G", "Gremlin" );
		    System.out.println(line);
		}   
		
		// Always close files.
		bufferedReader.close();         
	    }
	    catch(FileNotFoundException ex) {
		System.out.println(  "Unable to open file '" + 
				     fileName + "'");                
	    }
	    catch(IOException ex) {
		System.out.println("Error reading file '" 
				   + fileName + "'");                  
	    }
	}
    }
}