替换/替换bash中文件名列表中的子字符串列表(使用awk?)

替换/替换bash中文件名列表中的子字符串列表(使用awk?),bash,awk,substring,filenames,substitution,Bash,Awk,Substring,Filenames,Substitution,我发现,在我的>100.000个文件名中,我的分隔符\uuu也出现在不期望的位置,并扰乱了处理过程。因此,我想替换这些文件中的。。它们都在一个文件夹中。我试图使用awk FILENAME变量,但我不知道如何处理它来更改文件名本身。完整的文件名是 mg.reads.per.gene_Putative@polyhydroxyalkanoic@acid@system@protein@(PHA_gran_rgn)_A1.tsv mg.reads.per.gene_Phage@regulatory@

我发现,在我的>100.000个文件名中,我的分隔符
\uuu
也出现在不期望的位置,并扰乱了处理过程。因此,我想替换这些文件中的
。它们都在一个文件夹中。我试图使用awk FILENAME变量,但我不知道如何处理它来更改文件名本身。完整的文件名是

mg.reads.per.gene_Putative@polyhydroxyalkanoic@acid@system@protein@(PHA_gran_rgn)_A1.tsv   
mg.reads.per.gene_Phage@regulatory@protein@Rha@(Phage_pRha)_A1.tsv 
...
一般来说,第一个和最后一个
应该在那里,所有附加的都应该被替换。注意:附加值并不总是在括号中。我在名为
problems.txt的文件名中生成了一个包含这些有问题的子字符串的列表:

Putative@polyhydroxyalkanoic@acid@system@protein@(PHA_gran_rgn)
Phage@regulatory@protein@Rha@(Phage_pRha)
Phage@tail@protein@(Tail_P2_I)
Phd_YefM
pheT_bact:@phenylalanine--tRNA@ligase%2C@beta@subunit
...
在这里,您还想使用
@
作为不常见字符来获取:

mg.reads.per.gene_Putative@polyhydroxyalkanoic@acid@system@protein@(PHA@gran@rgn)_A1.tsv    
mg.reads.per.gene_Phage@regulatory@protein@Rha@(Phage@pRha)_A1.tsv 
...
如何使用此列表作为输入,仅更改与列表中的记录匹配的文件名?我尝试此方法来处理文件夹中的文件,并更改部分文件名(awk伪代码):


但是我不能指定我只希望在“问题记录”条目覆盖的区域内进行更改。我也不知道如何指定输出

这里有一个纯bash解决方案:

#!/bin/bash

# Loop over all files in the current directory
for i in *; do

  # Extract the part before the first _
  head="${i%%_*}"

  # Get the rest of the string
  tail="${i#*_}"

  # Extract the part after the last _
  rhead="${tail##*_}"

  # Extract the "middle" portion
  rtail="${tail%_*}"

  # Substitute _ with @ in the "middle"
  fixedrtail="${rtail//_/@}"

  # Rename files
  #echo -e "Renaming \"$i\" to \"$head_${fixedrtail}_$rhead\""
  mv $i "${head}_${fixedrtail}_${rhead}"
done

这将获取当前目录中的所有文件并对其进行重命名,以使除第一个和最后一个文件外的所有文件都替换为
@
。它使用了大量的参数扩展,您可以进行扩展。

对不起,StackOverflow不是这样工作的。形式为“我想做X,请给我提示和/或示例代码”的问题被认为是离题的。请访问并阅读,尤其是阅读。简而言之,你尝试了什么?我添加了我的方法,但它主要是伪代码,因为我实际上不知道如何进一步,这就是为什么我在这里发布完美,它有效。我会调查一下扩建项目。还有一件事(可能与此有关):在我所有的条目中,有一条被忽略了:
mg.reads.per.gene\u青霉素-binding@protein@4*_A5.tsv
我不明白人们如何在文件名中使用
*
;-)@疯狂的圣克劳斯我想这可能是因为我引用的不一致而被错误处理的。如果引用正确,Bash通常会处理得很好。我会修正并更新答案。
#!/bin/bash

# Loop over all files in the current directory
for i in *; do

  # Extract the part before the first _
  head="${i%%_*}"

  # Get the rest of the string
  tail="${i#*_}"

  # Extract the part after the last _
  rhead="${tail##*_}"

  # Extract the "middle" portion
  rtail="${tail%_*}"

  # Substitute _ with @ in the "middle"
  fixedrtail="${rtail//_/@}"

  # Rename files
  #echo -e "Renaming \"$i\" to \"$head_${fixedrtail}_$rhead\""
  mv $i "${head}_${fixedrtail}_${rhead}"
done