hannir hannir - 3 months ago 16
Bash Question

How to grep information?

What I have:

test
more text
@user653434 text and so


test
more text
@user9659333 text and so


I'd like to filter this text and finally get the following list as .txt file:

user653434
user9659333


It's important to get the names without "@" sign.
Thx for help ;)

Answer

Using grep -P (requires GNU grep):

$ grep -oP '(?<=@)\w+' File
user653434
user9659333

-o tells grep to print only the match.

-P tells grep to use Perl-style regular expressions.

(?<=@) tells sed that @ must precede the match but the @ is not included in the match.

\w+ matches one or more word characters. This is what grep will print.