0

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 ;)

hannir
  • 95
  • 8
  • Is the name always starting with an `@` and is that the only info in the file that starts with an `@`? – JNevill Aug 29 '16 at 17:27
  • Yes :) It always starts with @ and there is no more @ in the text, just at beginning. – hannir Aug 29 '16 at 17:28

2 Answers2

3

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.

To change the file in place with grep:

grep -oP '(?<=@)\w+' File >tmp && mv tmp File

Using sed

$ sed -En 's/^@([[:alnum:]]+).*/\1/p' File
user653434
user9659333

And, to change the file in place:

sed -En -i.bak 's/^@([[:alnum:]]+).*/\1/p' File

-E tells sed to use the extended form of regular expressions. This reduces the need to use escapes.

-n tells sed not to print anything unless we explicitly ask it to.

-i.bak tells sed to change the file in place while leaving a backup file with the extension .bak.

The leading s in s/^@([[:alnum:]]+).*/\1/p tells sed that we are using a substitute command. The command has the typical form s/old/new/ where old is a regular expression and sed replaces old with new. The trailing p is an option to the substitute command: the p tells sed to print the resulting line.

In our case, the old part is ^@([[:alnum:]]+).*. Starting from the beginning of the line, ^, this matches @ followed by one or more alphanumeric characters, ([[:alnum:]]+), followed by anything at all, .*. Because the alphanumeric characters are placed in parens, this is saved as a group, denoted \1.

The new part of the substitute command is just \1, the alphanumeric characters from above which comprise the user name.

Here, the s indicates that we are using a sed substitute command. The usual form

John1024
  • 97,609
  • 11
  • 105
  • 133
2

With GNU grep:

grep -Po '^@\K[^ ]*' file

Output:

user653434
user9659333

See: The Stack Overflow Regular Expressions FAQ

Community
  • 1
  • 1
Cyrus
  • 69,405
  • 13
  • 65
  • 117