Skip to content

Instantly share code, notes, and snippets.

@pbrisbin
Created February 11, 2011 20:43
Show Gist options
  • Select an option

  • Save pbrisbin/822986 to your computer and use it in GitHub Desktop.

Select an option

Save pbrisbin/822986 to your computer and use it in GitHub Desktop.
#!/bin/bash
parse_pid() {
local IFS=$'\n'
s_regex="^ $1:.*"
w_regex="^ $2:.*"
p_regex="^ $3:.*"
n_regex='^ [^ ]' # we've gone too far!
first=true
matching_pane=false
# assumption: no more than 100 lines to parse per session
grep -A 100 "$s_regex" | while read -r line; do
if $matching_pane; then
if [[ "$line" =~ $p_regex ]]; then
awk '{print $3}' <<< $line
break
fi
else
if [[ "$line" =~ $w_regex ]]; then
matching_pane=true;
continue
fi
fi
# we hit next session with no match
if ! $first && [[ "$line" =~ $n_regex ]]; then
echo 'no match' >&2
break
fi
first=false
done
}
# outputs 19131 when given http://paste.pocoo.org/raw/336726/ (minus markup)
parse_pid 0 4 0 < './test'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment