使用perl表达式批量重命名文件 [英] batch renaming of files with perl expressions

查看:81
本文介绍了使用perl表达式批量重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该是很多人的基本问题,但是我是一位没有编程背景的生物学家,所以请原谅.

This should be a basic question for a lot of people, but I am a biologist with no programming background, so please excuse my question.

我要做的是重命名大约100,000个具有现有代码名称的压缩数据文件(例如:XG453834.fasta.gz).我想将它们命名为易于理解和解析的名称(例如:Xanthomonas_galactus_str_453.fasta.gz).

What I am trying to do is rename about 100,000 gzipped data files that have existing name of a code (example: XG453834.fasta.gz). I'd like to name them to something easily readable and parseable by me (example: Xanthomonas_galactus_str_453.fasta.gz).

我尝试使用 sed 重命名 mmv 无济于事.如果我在一次性脚本中使用了这些命令中的任何一个,那么它们可以正常工作,只是当我尝试将变量合并到Shell脚本中时,我才遇到问题.我没有收到任何错误,只是名称没有更改,所以我怀疑这是一个I/O错误.

I've tried to use sed, rename, and mmv, to no avail. If I use any of those commands on a one-off script then they work fine, it's just when I try to incorporate variables into a shell script do I run into problems. I'm not getting any errors, just no names are changed, so I suspect it's an I/O error.

这是我的文件的样子:

#! /bin/bash
# change a bunch of file names
file=names.txt

while IFS=' '  read -r r1 r2;
do
    mmv ''$r1'.fasta.gz' ''$r2'.fasta.gz'
    # or I tried many versions of: sed -i 's/"$r1"/"$r2"/' *.gz
    # and I tried many versions of: rename -i 's/$r1/$r2/' *.gz

done < "$file"

...这是我的txt文件的第一行,其中有单个空格定界符:

...and here's the first lines of my txt file with single space delimiter:

    cat names.txt

   #find #replace 
   code1 name1
   code2 name2
   code3 name3

我知道我可以使用python或perl做到这一点,但是由于我一直在这里处理这个特定的脚本,因此我想找到一个简单的解决方案来修复此bash脚本并找出我在做错什么.非常感谢您提供的任何帮助.

I know I can do this with python or perl, but since I'm stuck here working on this particular script I want to find a simple solution to fixing this bash script and figure out what I am doing wrong. Thanks so much for any help possible.

另外,我尝试 cat 名称文件(请参见下面的Ashoka Lella的评论),然后使用 awk 进行移动/重命名.有些文件具有变量名(但总是以代码开头),因此我正在寻找查找&replace选项,仅用名称"替换代码"并保留文件名结构.

Also, I tried to cat the names file (see comment from Ashoka Lella below) and then use awk to move/rename. Some of the files have variable names (but will always start with the code), so I am looking for a find & replace option to just replace the "code" with the "name" and preserve the file name structure.

我怀疑我没有在perl表达式的单个刻度内转义变量,但是我倾倒了很多手册,但找不到解决方法.

I suspect I am not escaping the variable within the single tick of the perl expression, but I have poured over a lot of manuals and I can't find the way to do this.

推荐答案

我认为您不需要使用 mmv ,一个简单的 mv 即可.另外,无需指定 IFS ,默认设置将对您有效:

I don't think that you need to be using mmv, a simple mv will do. Also, there's no need to specify the IFS, the default will work for you:

while read -r src dest; do mv "$src" "$dest"; done < names.txt

我将变量名加双引号,因为通常认为这是一种好习惯,但是在这种情况下,任何一个文件名中的空格都会导致 read 不能按预期工作.

I have double quoted the variable names as it is generally considered good practice but in this case, a space in either of the filenames will result in read not working as you expect.

您可以在循环内的 mv 之前放置 echo ,以确保将执行正确的命令.

You can put an echo before the mv inside the loop to ensure that the correct command will be executed.

请注意,在文件 names.txt 中,已经包含了 .fasta.gz 后缀,因此您也不应将其添加到循环中.也许那是你的问题?

Note that in your file names.txt, the .fasta.gz suffix is already included, so you shouldn't be adding it inside the loop aswell. Perhaps that was your problem?

这篇关于使用perl表达式批量重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆