Created
March 12, 2014 11:43
-
-
Save cth/9505264 to your computer and use it in GitHub Desktop.
Select index of a particular named argument
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function nth() { | |
target=$1 | |
shift | |
idx=1 | |
for symbol in $@; do | |
if [ $symbol == $target ]; then | |
echo $idx | |
return | |
fi | |
idx=$(($idx+1)) | |
done | |
} | |
# Example: | |
# nth b a b c | |
# -> 2 | |
# | |
# Useful for, e.g., cut | |
# cut -f $(nth fieldofinterest $(head f.txt)) f.txt | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment