将文件名附加到行尾

时间:2016-03-31 20:25:31

标签: python bash perl sed fasta

我有很多fasta文件,想要将文件名添加到行尾(标题行)。我知道如何通过sed逐一进行,但寻找更好的解决方案来做许多文件。

File1.txt
>1
ACGTA

File2.txt
>2
GTCA

期待的是什么:

File1.txt
>1_File1
ACGTA

File2.txt
>2_File2
GTCA

这适用于在OSX中使用sed的单个文件:

sed -i.bak '/>.*$/ s/$/_File1/' File1.txt

由于

3 个答案:

答案 0 :(得分:1)

您可能需要根据文件名更改表达式,但基本上您想要遍历所有文件

    #!/bin/bash
    for file in *.txt
    do
      #just for echo the current 'file' var for testing
      echo "$file"
      sed -i.bak '/>.*$/ s/$/_"$file"/' $file
    done

答案 1 :(得分:0)

您可以使用Perl的就地编辑功能:

> interleavings ["abc", "123"]
["abc123","ab123c","ab12c3","ab1c23","a123bc","a12bc3","a12b3c","a1bc23","a1b23c","a1b2c3","123abc","12abc3","12ab3c","12a3bc","1abc23","1ab23c","1ab2c3","1a23bc","1a2bc3","1a2b3c"]
> interleavings ["a", "b", "c"]
["abc","acb","bac","bca","cba","cab"]
> permutations "abc" -- just for fun, to compare
["abc","bac","cba","bca","cab","acb"]

答案 2 :(得分:0)

你可以使用这个简单的for循环:

for f in *.txt; do
   sed -i '' 's/^>.*/&_'"${f%.*}"'/' "$f"
done

"${f%.*}"将从文件名中删除扩展名。