Skip to main content
add missing quotes, so works even if there are spaces in the line
Source Link
gmatht
  • 2.4k
  • 2
  • 20
  • 20

Your chunk function can be implemented in bash as follows.

chunk(){
    size=$1
    n=$2
    
    firstline=$((n*size))
    i=0
    while [ $i -lt $firstline ]
    do
        read -r junk || return
        i=$((i+1))
    done
    i=0
    while [ $i -lt $size ]
    do
        read -r str || return
        printf "%s\n" "$str"
        i=$((i+1))
    done
}

Note that results may be unexpected if files are added in between calls to find. So, you may want to implement a save_chunks/get_chunk API instead of your requested one, or do something like:

catn(){
    i=0
    while [ $i -lt $1 ] && read -r s
    do printf "%s\n" $s;i=$"$s";i=$((i+1))
    done
}
find -f . | 
    (catn 10; # shows first chunk of size 10 
     catn 10; # second chunk of size 10
     catn 10; # last of size < n 
     catn 10) # does nothing 

This should also be faster.

Your chunk function can be implemented in bash as follows.

chunk(){
    size=$1
    n=$2
    
    firstline=$((n*size))
    i=0
    while [ $i -lt $firstline ]
    do
        read -r junk || return
        i=$((i+1))
    done
    i=0
    while [ $i -lt $size ]
    do
        read -r str || return
        printf "%s\n" "$str"
        i=$((i+1))
    done
}

Note that results may be unexpected if files are added in between calls to find. So, you may want to implement a save_chunks/get_chunk API instead of your requested one, or do something like:

catn(){
    i=0
    while [ $i -lt $1 ] && read -r s
    do printf "%s\n" $s;i=$((i+1))
    done
}
find -f . | 
    (catn 10; # shows first chunk of size 10 
     catn 10; # second chunk of size 10
     catn 10; # last of size < n 
     catn 10) # does nothing 

This should also be faster.

Your chunk function can be implemented in bash as follows.

chunk(){
    size=$1
    n=$2
    
    firstline=$((n*size))
    i=0
    while [ $i -lt $firstline ]
    do
        read -r junk || return
        i=$((i+1))
    done
    i=0
    while [ $i -lt $size ]
    do
        read -r str || return
        printf "%s\n" "$str"
        i=$((i+1))
    done
}

Note that results may be unexpected if files are added in between calls to find. So, you may want to implement a save_chunks/get_chunk API instead of your requested one, or do something like:

catn(){
    i=0
    while [ $i -lt $1 ] && read -r s
    do printf "%s\n" "$s";i=$((i+1))
    done
}
find -f . | 
    (catn 10; # shows first chunk of size 10 
     catn 10; # second chunk of size 10
     catn 10; # last of size < n 
     catn 10) # does nothing 

This should also be faster.

Fixed head -n closing pipe, by changing to now catn function.
Source Link
gmatht
  • 2.4k
  • 2
  • 20
  • 20

Your chunk function can be implemented in bash as follows.

chunk(){
    size=$1
    n=$2
    
    firstline=$((n*size))
    i=0
    while [ $i -lt $firstline ]
    do
        read -r junk || return
        i=$((i+1))
    done
    i=0
    while [ $i -lt $size ]
    do
        read -r str || return
        printf "%s\n" "$str"
        i=$((i+1))
    done
}

Note that results may be unexpected if files are added in between calls to find. So, you may want to implement a save_chunks/get_chunk API instead of your requested one, or do something like:

catn(){
    i=0
    while [ $i -lt $1 ] && read -r s
    do printf "%s\n" $s;i=$((i+1))
    done
}
find -f . | 
    (head -ncatn 10; # shows first chunk of size 10 
     head -ncatn 10; # second chunk of size 10
     head -ncatn 10; # last of size < n 
     head -ncatn 10) # does nothing 

This should also be faster.

Your chunk function can be implemented in bash as follows.

chunk(){
    size=$1
    n=$2
    
    firstline=$((n*size))
    i=0
    while [ $i -lt $firstline ]
    do
        read -r junk || return
        i=$((i+1))
    done
    i=0
    while [ $i -lt $size ]
    do
        read -r str || return
        printf "%s\n" "$str"
        i=$((i+1))
    done
}

Note that results may be unexpected if files are added in between calls to find. So, you may want to implement a save_chunks/get_chunk API instead of your requested one, or do something like:

find -f . | 
    (head -n 10; # shows first chunk of size 10 
     head -n 10; # second chunk of size 10
     head -n 10; # last of size < n 
     head -n 10) # does nothing 

This should also be faster.

Your chunk function can be implemented in bash as follows.

chunk(){
    size=$1
    n=$2
    
    firstline=$((n*size))
    i=0
    while [ $i -lt $firstline ]
    do
        read -r junk || return
        i=$((i+1))
    done
    i=0
    while [ $i -lt $size ]
    do
        read -r str || return
        printf "%s\n" "$str"
        i=$((i+1))
    done
}

Note that results may be unexpected if files are added in between calls to find. So, you may want to implement a save_chunks/get_chunk API instead of your requested one, or do something like:

catn(){
    i=0
    while [ $i -lt $1 ] && read -r s
    do printf "%s\n" $s;i=$((i+1))
    done
}
find -f . | 
    (catn 10; # shows first chunk of size 10 
     catn 10; # second chunk of size 10
     catn 10; # last of size < n 
     catn 10) # does nothing 

This should also be faster.

Source Link
gmatht
  • 2.4k
  • 2
  • 20
  • 20

Your chunk function can be implemented in bash as follows.

chunk(){
    size=$1
    n=$2
    
    firstline=$((n*size))
    i=0
    while [ $i -lt $firstline ]
    do
        read -r junk || return
        i=$((i+1))
    done
    i=0
    while [ $i -lt $size ]
    do
        read -r str || return
        printf "%s\n" "$str"
        i=$((i+1))
    done
}

Note that results may be unexpected if files are added in between calls to find. So, you may want to implement a save_chunks/get_chunk API instead of your requested one, or do something like:

find -f . | 
    (head -n 10; # shows first chunk of size 10 
     head -n 10; # second chunk of size 10
     head -n 10; # last of size < n 
     head -n 10) # does nothing 

This should also be faster.