重命名匹配txt文件中字符串的文件
我试图根据匹配项将多个文件重命名为.txt文件 然后,我的文件是
GCF_000698265.1_ASM69826v1_genomic.gff.gz
GCF_000785125.1_ASM78512v1_genomic.gff.gz
GCF_000934565.1_ASM93456v1_genomic.gff.gz
GCF_000963495.1_ASM96349v1_genomic.gff.gz
我的标签分离的txt文件,看起来像这样:
GCF_000698265.1_ASM69826v1 Pseudomonas_str1
GCF_000785125.1_ASM78512v1 Pseudomonas_str2
GCF_000934565.1_ASM93456v1 Pseudomonas_str3
GCF_000963495.1_ASM96349v1 Pseudomonas_str4
因此,对于匹配文件第一列的文件名,我想将文件重命名为第二列。 我试图理解如何将MV和尴尬吹进,但我迷路了。 我希望我所需的输出看起来像这样:
Pseudomonas_str1_genomic.gff.gz
Pseudomonas_str2_genomic.gff.gz
Pseudomonas_str3_genomic.gff.gz
Pseudomonas_str4_genomic.gff.gz
有人可以帮忙吗? 我希望我很清楚,非常感谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因此,我创建了一个合成的测试集,它有些可观,并且有意地制作了 1/7th < / strong>匹配,并且那里
在任何地方都没有重复,因为合成文件名是
所有这些都基于唯一的素数列表,并且文件也按改为顺序进行。
254923
19113991 19113991 test_rename_output_2b.txt
254923
15545069 15545069 test_need_need_need_to_to_to_to_to_to_ename_ename_eneame_2.txt
此解决方案的优点是,它已经预先格式化用于使用MV命令(示例输出)直接重命名:
为了安全地播放它,我已经预先付费
not oftrite
akano-clobber
aka-n
在所有重命名命令中标记,可以将其直接发送到轻量的轻度上,例如dash
以执行而无需执行进一步的文件名操纵。我猜是可以接受的性能 -
mawk2
1.218 secs
完成所有步骤(包括将最终输出文件写入磁盘)。So i create a synthetic test-set that's somewhat sizable, and intentionally only made 1 / 7th of them match, and there
are no duplicates anywhere, since the synthetic file names are
all based on a unique list of primes, and the files are also in shuffled order.
254923
19113991 19113991 test_rename_output_2b.txt
254923
15545069 15545069 test_need_to_rename_2.txt
The advantage of this solution is that it's already pre-formatted for direct renaming using mv command (sample output) :
To play it safe, I've pre-pended the
don't overwrite
akano-clobber
aka-n
flag in all the renaming commands, which one can directly send into something light weight likedash
to execute without requiring further filename manipulation.Performance is acceptable I suppose -
mawk2
took1.218 secs
to complete all steps (inclusive of writing final output file to disk).使用
sed
和bash
,假设txt文件使用awk命名为'rename.txt':
此处的密钥是将“ MV”插入到每行的开头并执行。
最后一个解决方案不使用任何外部工具,只需bash:
更新
基于阿尔贝托的更新问题
,以下是更改:使用:
注意:
; s/$/_ genomic.gff.gf.gz/
表达式>说:搜索行的末尾,并将“ _genomic.gff.gz”附加到其中。只有在每行中没有尾随空间的情况下,这才能起作用。使用尴尬:
使用bash:
Using
sed
andbash
, assuming the txt file is named 'rename.txt'Using awk:
The key here is to insert "mv " to the beginning of each line and execute.
This last solution does not use any external tool, just bash:
Update
Based on Alberto's updated question, here are the changes:
Using sed:
Note: The
;s/$/_genomic.gff.gz/
expression said: search the end of the line and append "_genomic.gff.gz" to it. This will work only if you don't have trailing spaces in each line.Using awk:
Using Bash:
您可能会使用尴尬来准备一系列命令,然后将其用作
bash
的标准输入。请注意,您的案件是特定的,因为文件名中没有空格,如果空格被命名,则您可以简单地使用
mv
进行预置行,例如,如果上述文件被命名为renaming.txt < /code>然后您可能会这样做
,但是如果任何名称中都有空间,则会失败。如果允许空格,我建议使用
python
(如果您使用Linux Machine,可能已安装)以下方式,创建文件renaMer.py
withwhere with code> renaming.txt 是带有2个剪切列的文件名,保留当前名称和所需的名称,然后使用它
的工作方式如下:
open
srenaming.txt
用于读取(r
),每行都会在tab字符处进行抛弃式尾随tagring whitespace(newlines)和拆分行,第一部分转到src
,第二部分,第二部分到dst
然后在。 /代码> 功能。您可以选择其他语言,最好是具有用于管理文件的功能,因为这将使为此任务开发代码。
You might use AWK to prepare series of commands which then you use as standard input for
bash
. Be warned that your caseis specific as there are not spaces in filenames, if spaces are prohibitied in names then you might simply prepend lines with
mv
for example if said files is namedrenaming.txt
then you might dohowever this will fail if there is space in any name. If spaces are allowed then I suggest to use
python
(which is likely installed if you use linux machine) following way, create filerenamer.py
with following contentwhere
renaming.txt
is name of file with 2 tab-sheared columns holding current name and desired name then use it as followsHow it works:
open
srenaming.txt
for reading (r
) for each line it does jettison trailing whitespaces (newlines) and split line at TAB character, 1st part goes tosrc
, 2nd todst
which are then used inos.rename
function.You might select other language for that, preferably which has function for managing files, as this will made developing code for this task easier.