How to split a string depends on a pattern in other column (UNIX environment) How to split a string depends on a pattern in other column (UNIX environment) unix unix

How to split a string depends on a pattern in other column (UNIX environment)


If I understood the question correctly, this awk should do it:

awk '( substr($6, 1, 1) != $1 || substr($6, length($6), 1) != $2 ) && i = index($6, $2$1) { $6 = substr($6, i+1) substr($6, 1, i)  }1' OFS=$'\t' data

You basically want to rotate the string so that the beginning of the string matches the char in $1 and the end of the string matches the char in $2. Strings that cannot be rotated to match that condition are left unchanged, for example:

A    B    3    3    -    BCAAB


You can try this awk, it's not perfect but it give you a starting point.

awk '{i=(match($6,$1));if(i==1)print;else{a=$6;b=substr(a,i);c=substr(a,1,(i-1));$6=b c;print}}' OFS='\t' infile


gawk 'BEGIN{    OFS="\t"}$6 !~ "^"$1".*"$2"$" {    $6 = gensub("(.*"$2")("$1".*)", "\\2\\1", 1, $6)}{print}' input.txt

Output

V   I   280     6   -   VRSSAIN   V   2739    7   -   NATASAVA   R   203     5   -   AEERRQ   A   2517    7   -   QSTPSPAS   S   1012    5   -   SGGGSL   A   281     11  -   LSAGSLAAEPA