I need help with extracting a string between the "@" symbol and a space " " in Bash.
I'm using Python Twitter Tools, and the output is like this:
430438229200740352 2014-02-03 14:30:45 CST <HorizonAwon> @SawBlastt @WereAutomatic 101 for me to join as well
I need to extract the two strings:
SawBlastt
WereAutomatic
I also need to set them each as a separate variable. I've tried messing around with sed and grep, but no successful results. I'm really stuck on this. Help is very appreciated. Thanks!
You can use:
s='430438229200740352 2014-02-03 14:30:45 CST <HorizonAwon> @SawBlastt @WereAutomatic 101 for me to join as well'
grep -oP '@\K[^ ]*' <<< "$s"
SawBlastt
WereAutomatic
Another gnu grep command, that I used mostly.
grep -Po "(?<=@)[^ ]*" file
BASH has its own regex matching capabilities that will work.
s="430438229200740352 2014-02-03 14:30:45 CST <HorizonAwon> @SawBlastt @WereAutomatic 101 for me to join as well"
if [[ $s =~ @([A-Za-z]+)\ @([A-Za-z]+) ]]; then
echo ${BASH_REMATCH[1]} ${BASH_REMATCH[2]}
fi
To explain, here's what man bash says:
The element of BASH_REMATCH with index n is the portion of the string matching the nth parenthesized subexpression.