Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grep Print Only After Match [duplicate]

Tags:

grep

bash

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...

like image 652
user1709294 Avatar asked Oct 01 '12 23:10

user1709294


3 Answers

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
like image 82
Gilles Quenot Avatar answered Oct 28 '22 03:10

Gilles Quenot


@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
like image 40
Tim Pote Avatar answered Oct 28 '22 05:10

Tim Pote


Stop using grep.

$ awk -F/ '$2 == "player" { print $3 }' input.txt
ABc12
ABC321
EGF987
egf751
like image 27
Ignacio Vazquez-Abrams Avatar answered Oct 28 '22 04:10

Ignacio Vazquez-Abrams