#!/bin/bash # This bash script re-formats the lines of the standard input to a # format specified by the command line arguments. It is a simple loop # around the printf command of bash(1). # The standard input consists of lines that have fields separated by # white space, as implicitly defined by the read-function in bash(1). # The command line arguments are a list of C-style format specifications # for the printf command in bash(1) for each of the input lines. These # are automatically augmented by a line feed as a last specification. # If the input lines have more fields than covered by the number of # command line arguments, the format-list is worked through repeatedly # from the left to the right, as specified by the printf function in bash(1). # Examples: # cFmt %20s %20s %.0s %.0s %.0s < myFile # re-prints the myFile and puts the first entry in its lines into a field # of 20 characters, the second entry into a filed of 20 characters, and the # 3rd to 5th field are suppressed. The 6th and 7th field are again emitted # in 20-character-wide fashion. # # cFmt %23.16e < myFile # re-prints the myFile and prints all entries in a floating-point # exponential format. # # cFmt %23.16e "|" %23.16e "|" < myFile # re-prints the myFile and prints all entries in a floating-point # exponential format, a maximum of 2 values per line, adding vertical bars in # between. # see also: bash(1), printf(1), printf(3), cut(1), paste(1) # Richard J. Mathar # 2007-10-05 # the format is the comand line arguments, concatenated fmt=$* # debugging # echo format $fmt # a bash array to handle a variable number of fields declare -a inl while true ; do # read a single line of stdin read -a inl ; # if there was no further input, exit if [[ $? -ne 0 ]] ; then break ; fi printf "$*\n" ${inl[@]} ; done