2010-03-11, 04:10(-08), Javier Montoya: [...] > I'm a newbie with bash programming and I'm trying to process a file > into smaller > chunks. Let's say I've to process the file below ('X' and 'Y' are any > possible float number): > > 0 0 0 0 0 0 > 0 0 0 0 0 0 > X X X X X 1.70 > 0 0 0 0 0 0 > X X X X X 1.60 > 0 0 0 0 0 0 > X X X X X 1.30 > Y Y Y Y Y 1.90 > 0 0 0 0 0 0 > 0 0 0 0 0 0 > X X X X X 2.10 > > The lines containing 0’s can be thought as being delimiters. I need to > find between the delimiters, which line has the highest value in the > last column (column#6), and, just output the corresponding line. Does > anybody could shed some light on it? An example of the desired output > would be: > > X X X X X 1.70 > X X X X X 1.60 > Y Y Y Y Y 1.90 > X X X X X 2.10 > > Is it possible to obtain such a result using bash scripting? [...]
Yes, you don't even need the syntax of your script to be bash-specific: awk ' function end_block() { if (max) print max_line max=0 } $NF == 0 { end_block() next } $NF > max || !max { max=$NF max_line=$0 } END {end_block()}' < the-file Should work with any shell of the Bourne (including bash or any standard Unix shell) or rc families. -- Stéphane