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