Skip to content

Instantly share code, notes, and snippets.

@mrrusof
Created July 4, 2017 22:41
Show Gist options
  • Select an option

  • Save mrrusof/f252d8633fd6257777d620b66326c0d4 to your computer and use it in GitHub Desktop.

Select an option

Save mrrusof/f252d8633fd6257777d620b66326c0d4 to your computer and use it in GitHub Desktop.
In Bash, given a matrix, return element in given row & col
#!/bin/bash
function element { r=$1; c=$2; head -n $r | tail -n 1 | awk "{ print \$$c }"; }
# Example usage:
#
# $ echo -e "1 2 3\n4 5 6\n7 8 9" | element 2 2
# 5
# $ echo -e "1 2 3\n4 5 6\n7 8 9" | element 2 3
# 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment