print every 20 lines the lowest number

Hello all,

How can I find the lowest number every 10 lines? For example i have a list

name1
-0.1
name2
2
name3
3
name4
-3
name5
1
name6
2
name7
34
name8
34
name9
21
name10
1
name11
2
name12
65
name13
5
name14
7
name15
8
name16
0.5
name17
-32
name18
-7
name19
0
name20
-142

and I want to print

name4 \t -3
name20 \t -142

is there a nice way to do this?

Don't know if you will consider this nice:

awk '!((NR-1)%20){n[int((NR-1)/20)]=$0;getline;a[int((NR-2)/20)]=$1}(NR%2){t=$0;getline;if (a[int((NR-2)/20)]>$1) {a[int((NR-2)/20)]=$1;n[int((NR-2)/20)]=t}};END{for (i=0;i<NR/20;i++){print n"\t"a}}' file
1 Like

And another one:

awk 'END {
  if ( !( c % l ) && nr != NR )
     print n, m
    }
{
  if ( /^[0-9-]/ ) {
    m == "" && m == 0 && m = $1
    $1 < m && m = $1
    if ( !( ++c % l ) ) {
      print n, m
      m = $1; nr = NR	  
      }
    }
  else n = $0
  }' OFS='\t' l=10 infile
1 Like

Try if the below one works..

awk '/^[a-z]/{s=$0;getline;{if(x>int($0)){c=s;x=$0}};++n} n==10{print c,x;n=""}' inputfile

wow! that was quick! Thank both fro the scripts - although bartus' script is a bit faster, radoulov's script is more understandable by me!

thank you both guys i appreciate it a lot, and i really like that the community is helping each other.

---------- Post updated at 07:55 AM ---------- Previous update was at 07:54 AM ----------

no, it is not working - giving me empty lines

OK,
the line that is checking if the variable m is defined should be like this:

m == "" && m == 0 && m = $1

Fixed some bugs:

awk 'END {
  if ( !( c % l ) && nr != NR )
     print n, m
    }
{
  if ( /^[0-9-]/ ) {
    m == "" && m == 0 && m = $1
    $1 < m && m = $1
    if ( !( ++c % l ) ) {
      print n, m
      m = $1; nr = NR	  
      }
    }
  else n = $0
  }' OFS='\t' l=10 infile

Its works at my end based on your input file format in post# 1.

> awk '/^[a-z]/{s=$0;getline;{if(x>int($0)){c=s;x=$0}};++n} n==10{print c,x;n=""}' inputfile
name4 -3
name20 -142