#!/bin/bash function station() { a="$@" s=$(jq -nr --arg v "$a" '$v|@uri') data=$(curl -s "http://infopasazer.intercity.pl/?p=stations&q=$s") IFS=$'\n' links=($(echo "$data" | grep -Poh "\?p\=station\&id\=[0-9]+")) names=($(echo "$data" | grep -i ">$@" | tr -d '\r' | sed -s 's///g;s/ //g;s/<\/span>//g')) if [[ ${#links[@]} -gt 1 ]]; then echo "Multiple stations found! Narrow your search down, pretty please" fi for (( i=0; i<${#links[@]}; i++ )); do echo "${links[$i]}:${names[$i]}" done } station="$(station "$1")" if [[ $(echo "$station" | grep -i "multiple") != '' ]]; then echo "$station" exit 0 fi url="http://infopasazer.intercity.pl/$(echo $station | grep -Poh "^[^:]+")" data="$(curl $url | tr -d '\r' | tr -d '\n' | grep -Poh "")" IFS=$'\n' trains=$(echo $data | grep -Poh '.*?
.*?
') links=($(echo $data | grep -Poh "\?p\=train&id\=[0-9]+")) names=($(echo "$trains" | grep -Poh ">.*
.*" | sed -s 's// /g;s/<\/a>//g;s/^>//g')) for (( i=0; i<${#links[@]}; i++ )); do echo ${links[$i]}: ${names[$i]} done