Welcome to the site.
I think you are mixing up "wildcard characters" (a.k.a. "shell globs") with regular expressions in your use of sed.
Your intention clearly is to replace the pattern "underscore, followed by any number of letters and digits, followed by .gc_corrected.bam" by _input.gc_corrected.bam. Unfortunately, your sed expression is flawed in two ways:
- Your matching part uses the shell-typical "wildcard character" notation. However,
sed expects a regular expression here, and in regular expressions, the * does not mean "any string of zero or more characters", but "zero or more repetitions of the previous character", so that your expression would replace any pattern consisting of "zero or more underscores, followed by .gc_corrected.bam" with the replacement _input.gc_corrected.bam. That is why in your case, simply the last underscore before the filename suffix is replaced with _input.
- Even so, if you have multiple underscores in your actual filename, a pattern matching "a string consisting of any number of characters" would also include underscores, which may lead to undesired behaviour on the length of the matched string. In particular, regular expressions are greedy, and if not constructed carefully, you might end up replacing the entire
_mESC_Rep_1_H3K27Ac part of the filename by _input.
In your case, the correct regular expression would be:
sed 's/_[^_]+\.gc_corrected\.bam/_input.gc_corrected.bam/g'
This would replace a string, starting with an underscore _, followed by one or more characters that are not underscores ([^_]+), followed by .gc_corrected.bam, with your substitution _input.gc_corrected.bam.
Note also that in regular expressions, the . stands for "any single character" (which in shell globs would be represented by the ?), so if you want to match a literal ., you have to escape it. This is of course not necessary in the replacement string as that is not a regular expression.
sedis likely the wrong tool here anyway. How aboutrename?renamecommand, the one written by Larry Wall (apt install rename). It issedfor file-names._*means "many_". Instead ofsed 's/_*.gc....trysed 's/_.*.gcorsed 's/_[^_]*.gc...