Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cut for variable delimiter - Bash [duplicate]

Tags:

bash

unix

cut

I want to get the second column of a file where separator between column 1 and column 2 is of variable length. For example:

A1_KOG1494_________AAMFAARSFSERK
A2_KOG1494_______AAKFALRSFSERK
A3_KOG1494___________AAKFALRSFSCCK

Thank's for your help

like image 810
Muramasa Avatar asked May 15 '14 10:05

Muramasa


1 Answers

You can use awk with custom field separator:

s='A1_KOG1494_________AAMFAARSFSERK'
awk -F '_{2,}' '{print $2}' <<< "$s"
AAMFAARSFSERK

awk -F '_' '{print $NF}' <<< "$s"
AAMFAARSFSERK

Another example:

awk -F '_{2,}' '{print $2}' <<< "A3_KOG1494___________AAKFALRSFSCCK"
AAKFALRSFSCCK
like image 195
anubhava Avatar answered Sep 21 '22 02:09

anubhava