Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

341
Views
Extracting A String Between Two Characters in Bash

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!

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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
over 4 years ago · Santiago Trujillo Report

0

Another gnu grep command, that I used mostly.

grep -Po "(?<=@)[^ ]*" file
over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!