Fetching a string between two characters in Bash

I need help extracting a line between the "@" character and the space bar "" in Bash.

I am using Python Twitter Tools and the output is as follows:

430438229200740352 2014-02-03 14:30:45 CST <HorizonAwon> @SawBlastt @WereAutomatic 101 for me to join as well

I need to extract two lines:

Sawblastt

WereAutomatic

I also need to set them as a separate variable. I tried communicating with sed and grep, but no successful results. I am really stuck on this. Help is much appreciated. Thank!

+3
source share
4 answers

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
+4
source

Another gnu grep command that I used mostly.

grep -Po "(?<=@)[^ ]*" file
+2
source

BASH , .

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

, man bash:

BASH_REMATCH n , n- .

+1

OP,

$ cat foo.txt
430438229200740352 2014-02-03 14:30:45 CST <HorizonAwon> @SawBlastt @WereAutomatic 101 for me to join as well

$ set $(awk '{print $6,$7}' FS='[ @]+' foo.txt)

$ echo $1 $2
SawBlastt WereAutomatic
0

All Articles