kamalghaz
kamalghaz

Reputation: 13

sed:remove characters after matched Nth characters

I would like to simplified my database, removing unwanted information however cannot find ways to do it. Here is example of my file, In 6th column I would like to remove all characters before and after BPSS. The number which linked to before and after BPSS must be retained. Table example

bactNOG ENOG410884P     2       2       M       379066.GAU_3228,272560.BPSS2320
bactNOG ENOG4108K54     20      20      S       240016.VspiD_010100004855,530564.Psta_1282,349741.Amuc_1603,272560.BPSS1860,761193.Runsl_3432,320771.Cflav_PD6017 
bactNOG ENOG4108PSW     33      31      E       1048834.TC41_0741,749927.AMED_4235,272560.BPSS0750,479433.Caci_2010,521098.Aaci_0763,220664.PFL_3212,208964.PA1485
bactNOG ENOG4108Q8E     10      9       G       272568.Gdia_0844,349163.Acry_2602,366394.Smed_0916,266834.SMc02021,266835.mll7376,457421.CBFG_05672,272560.BPSS2071

I would like simplified the table into this

bactNOG ENOG410884P     2       2       M       272560.BPSS2320
bactNOG ENOG4108K54     20      20      S       272560.BPSS1860
bactNOG ENOG4108PSW     33      31      E       272560.BPSS0750
bactNOG ENOG4108Q8E     10      9       G       272560.BPSS2071

I know some few command in sed, but those command unable to help me. I've tried sed command like this

sed 's/BPSS[0-9][0-9][0-9][0-9].*/BPSS[0-9][0-9][0-9][0-9]/g' file

But the numbers after BPSS were changed. I want to retained the (BPSS) numbers.

Thanks in advance

Kamal

Upvotes: 0

Views: 59

Answers (1)

Ed Morton
Ed Morton

Reputation: 204035

$ sed 's/[^[:space:]]*,\([0-9]*\.BPSS[0-9]*\).*/\1/' file
bactNOG ENOG410884P     2       2       M       272560.BPSS2320
bactNOG ENOG4108K54     20      20      S       272560.BPSS1860
bactNOG ENOG4108PSW     33      31      E       272560.BPSS0750
bactNOG ENOG4108Q8E     10      9       G       272560.BPSS2071

Upvotes: 1

Related Questions