tom
tom

Reputation: 199

perl regex to extract a specifc word

I have the following exmaple of a text file:

AFUA_2G08360|pyrG
AFUA_2G12630
gel1|bgt2|AFUA_2G01170

and I wish to do a regex to filter out AFUA_2G08360, AFUA_2G12630, AFUA_2G01170 using perl -l -ne in unix command line.
How would you suggest to do that?

Upvotes: 1

Views: 137

Answers (5)

Vijay
Vijay

Reputation: 67211

use

perl -pe 's/.*(AFUA_[0-9a-zA-Z]*).*$/\1/' your_file

tested:

> cat temp
    AFUA_2G08360|pyrG
    AFUA_2G12630
    gel1|bgt2|AFUA_2G01170
> perl -pe 's/.*(AFUA_[0-9a-zA-Z]*).*$/\1/' temp
AFUA_2G08360
AFUA_2G12630
AFUA_2G01170

Upvotes: 0

DhruvPathak
DhruvPathak

Reputation: 43235

AFUA_[0-9A-Za-z]{7} 

See here : http://regexr.com?328gj

Command line :

user@mch:/tmp$ cat input.txt 
AFUA_2G08360|pyrG
AFUA_2G12630
gel1|bgt2|AFUA_2G01170

user@mch:/tmp$ cat input.txt | perl -lne "@matches = /AFUA_[0-9A-Za-z]{7}/g; print join("\n", @matches)";
AFUA_2G08360
AFUA_2G12630
AFUA_2G01170

Upvotes: 0

Summer_More_More_Tea
Summer_More_More_Tea

Reputation: 13356

Here is a doable one-liner for your example input.

cat data | perl -l -e 'while (<>) {s/.*(AFUA_[^\|]*).*/\1/g; print}'

Upvotes: 0

Andr&#233;
Andr&#233;

Reputation: 860

Try this expression:

/(AFUA_2G\d+)/g

Upvotes: 0

rezna
rezna

Reputation: 2243

why not using 'sed' with something like

sed 's/AFUA_2G\d{5}//'

Upvotes: 1

Related Questions