The sed command uses a regular expression (regex), which if you don't know is an advanced pattern-matching language. I highly recommend learning at least the basics of it if you plan on doing much scripting. There are plenty of tutorials available on the net.
Here's a break down the command I gave you.
sed -r "s/([0-9]+_)[^_]+(_*)/\1$2\2/"
The -r option turns on regex. Note also that the expression is enclosed in double-quotes, since you need to be able to expand the shell variable in it.
([0-9]+_)
The brackets will match a range of characters, in this case "0-9". The plus sign means "match one or more of the previous character", letting it match any string of numbers. Then I added the underscore. Then I put the whole thing in parentheses to save it for the output. More on that later.
[^_]+
One difficulty with regex is that it's greedy. If you use a simple wildcard, it will keep going until it finds the last matching character available, so if your string has multiple underscores, it wouldn't stop until it hits the last one. So instead I use a negation ^ in range brackets means "not", so in this case, "match any number of characters except the underscore", until you hit the next character in the expression. We don't need to save this part, so no parentheses here.
(_*) --fixed to--> (_.*)
Actually, this is a mistake on my part. There should be an additional period here.
When the expression reaches the next underscore, save it, and everything else following it, in a second parentheses. The period is a wildcard meaning "any character at all", and * is similar to +, but will match zero or more of the previous character. So in regex ".*" means match everything.
\1$2\2
The replacement function is easy. Each set of parentheses in the matching expression is referred to by a \n. So \1 outputs the contents of whatever the first parentheses matches, and \2 outputs the second parentheses. Then just put your variable in the middle, and you have your final string.
Now your real-life example might actually be a bit easier to do, since all you want to do is remove a fixed pattern from the string. Assuming the pattern is fixed, of course. That's the problem with pattern matching, if there's no common pattern, there's no easy way to set up a matching expression
.
Code:
echo $FILE| sed -r "s/_[0-9]{8}.nii_Corrected.nii.gz//"
({8} is a regex specifying the exact number of the previous character to match, 8 in this case)
Or using bash parameter substitution (which doesn't have range brackets).
Code:
${FILE/_[1-2][0-9][0-9][0-9][0-1][0-9][0-3][0-9].nii_Corrected.nii.gz/}
Notice how I customized the number ranges to match only the ones that appear in dates. You can add in $2 or whatever in the replacement side of the expression, of course, since removing the pattern from the shorter of the examples you gave leaves the files with no ending part.