10

I used grep that outputs a list like this

/player/ABc12
/player/ABC321
/player/EGF987
/player/egf751

However I want to only give the name of the players such ABC321, EFG987, etc...

Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110
user1709294
  • 1,675
  • 5
  • 18
  • 21
  • grep only shows you the lines that contain what you found. usually you'd use awk/sed to filter things so you only get the sub-parts of the line. – Marc B Oct 01 '12 at 23:56

4 Answers4

24

Start using grep :

$ grep -oP "/player/\K.*" FILE
ABc12
ABC321
EGF987
egf751

Or shorter :

$ grep -oP "[^/]/\K.*" FILE
ABc12
ABC321
EGF987
egf751

Or without -P (pcre) option :

$ grep -o '[^/]\+$' FILE
ABc12
ABC321
EGF987
egf751

Or with pure bash :

$ IFS=/ oIFS=$IFS
$ while read a b c; do echo $c; done < FILE
ABc12
ABC321
EGF987
egf751
$ IFS=$oIFS
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
13

@sputnick has the right idea with grep, and something like that would actually be my preferred solution. I personally immediately thought of a positive lookbehind:

grep -oP '(?<=/player/)\w+' file

But the \K works perfectly fine as well.

An alternative (somewhat shorter) solution is with sed:

sed 's:.*/::' file
Tim Pote
  • 27,191
  • 6
  • 63
  • 65
5

Stop using grep.

$ awk -F/ '$2 == "player" { print $3 }' input.txt
ABc12
ABC321
EGF987
egf751
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

One way using GNU grep and a positive lookbehind:

grep -oP '(?<=^/player/).*' file.txt

Results:

ABc12
ABC321
EGF987
egf751
Steve
  • 51,466
  • 13
  • 89
  • 103