sed -e '
;# |--1---| |-2-|
Place the markers around the ID string and grab the portion before the first _
and replace the whole line with these values.
You said you wanted the ID extracted which is lying between the 1st and 2nd occurrences of ::
Step-1: Place a marker (usually a \n) around the region of interest:
This is how the pattern space looks after the above tranformation
Step-2: Extract the ID which is the thing in between the two \ns as well as the string to the
left of the 1st occurrence of _
;# |------| |---|
;# \1 \2
[^_] => matches any char but an underscore
[^_]* => matches 0 or more non underscore char(s)
\([^_]*\) => store what was matched into a memory, recallable as \1
^\([^_]*\) => anchor your matching from the start of the string
.*\n => go upto to the rightmost \n you can see in the string
\n\(.*\)\n => Ooops!! we see another \n, hence we need to backtrack to
the previous \n position and from there start moving right again
and stop at the rightmost \n. Whatever is between these positions
is the string ID and is recallable as \2. Since the \ns fall outside
the \(...\), hence they wouldn't be stored in \2.
.* => This is a catchall that we stroll to the end of the string after
starting from the rightmost \n position and do nothing with it.
So our regex engine has matched against the input string it was given in
the pattern space and was able to store in two memory locations the data
it was able to gather, viz.: \1 => stores the string portion which is in
between the beginning of the pattern space and the 1st occurrence of the
\2 => store the string portion which is in between the 1st and 2nd
occurrences of :: in the pattern space.
\1 = ID1
\2 = TRINITY_DN120587_c0_g1_i1
Now comes the replacement part. Remember that the regex engine was able to scan
the whole of pattern space from beginning till end, hence the replacement
will effect the whole of the pattern space.
\2[\1] => We replace the matched portion of the pattern space (in our case it
happens to be the entire string) with what has been stored in
the memory \2 literal [ memory \1 literal ]
leading to what we see below:
In other words, you have just managed to turn the pattern space from:
into the following: