For a given input as provided, this sed
expression seems to do what you ask:
$ cat input
`>TRINITY_DN75270_c3_g2::TRINITY_DN75270_c3_g2_i4::g.22702::m.22702 [sample]`
$ sed 's/^.*::\([A-Z_0-9a-z]*\)::.*\[\(.*\)\].*/\1[\2]/' input
TRINITY_DN75270_c3_g2_i4[sample]
The magic is in using regular expression groups, and two backreferences to reconstruct the desired output. To expound:
NODE EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
.* any character except \n (0 or more times
(matching the most amount possible))
:: '::'
\( group and capture to \1:
[A-Z_0-9a-z]* any character of: 'A' to 'Z', '_', '0'
to '9', 'a' to 'z' (0 or more times
(matching the most amount possible))
\) end of \1
:: '::'
.* any character except \n (0 or more times
(matching the most amount possible))
\[ '['
( group and capture to \2:
.* any character except \n (0 or more times
(matching the most amount possible))
) end of \2
\] ']'
.* any character except \n (0 or more times
(matching the most amount possible))
So \1
is the first key you wanted to extract, and \2
is whatever is in the square braces afterward. Is is then reconstructed by \1[\2]/
, creating your desired output.