#!/bin/bash
#
# filterline keeps a subset of lines of a file.
#
# cf. http://unix.stackexchange.com/q/209404/376
#
set -eu
set -o pipefail

if [ "$#" -ne 2 ]; then
    echo "Usage: filterline FILE1 FILE2"
    echo
    echo "FILE1: one integer per line indicating line number, one-based"
    echo "FILE2: input file to filter"
    exit 1
fi

[ ! -f "$1" ] && echo "file not found: $1" && exit 1
[ ! -f "$2" ] && echo "file not found: $2" && exit 1

LIST="$1" LC_ALL=C awk '
  function nextline() {
    if ((getline n < list) <=0) exit
  }
  BEGIN{
    list = ENVIRON["LIST"]
    nextline()
  }
  NR == n {
    print
    nextline()
  }' < "$2"
