0

I've got the following problem regarding to sed command. What I wish to do is:

sed '1 d' filename.fa | sed "1i\>filename\n" > filename_Edited.fa

where the file frist line is replaced by ">filename". This is done in 2 steps: deleting first line and then inserting a new one which contains the desired ">text". The command works as it is wirtten above if it is typed directly into the console (w/o variables). However, I need this command integrated in the following script where filename is now depending on a variable:

#!/bin/bash

CODE=`cut -c 7-21 Data.txt`

cd ../FASTA_SEC/
 for i in ${CODE}
 do
 sed '1 d' ${CODE}.fa | sed "1i\>${CODE}\n" > ${CODE}_Edited.fa
 done

I get the following error when this script is ran for each for loop iteration:

try: line 8: ${CODE}_Edited.fa: ambiguous redirect

I dont get whats wrong in sed sintaxis orin the overall script, Aparently it should work but it does not. Any clue?

I have also tried to run the script without pipped part, only executing the sed which deletes first line from the text:

#!/bin/bash

CODE=`cut -c 7-21 Data.txt`

cd ../FASTA_SEC/
 for i in ${CODE}
 do
 sed '1 d' ${CODE}.fa > ${CODE}_Edited.fa
 done

However, it returns the former error again!

Thank you for yr help!!

**.fa format are known as fasta which are a kind of plain text format used in DNA sequences

  • for i in ${CODE} -> you probably want to use $i (or ${i} if you prefer) in the loop body, not ${CODE}. I guess this is the source of your "ambiguous redirect" error – Aaron Nov 29 '16 at 16:08
  • yush true want an error! i got stuk there XD oki the script Works with ${i} lel. – Neuls Nov 29 '16 at 16:27
2

It might be simpler to not use sed for the prepend, but rather cat:

#!/bin/bash
header=$(mktemp)
scratch=$(mktemp)
trap "rm -f $header $scratch" EXIT

cd ../FASTA_SEC/
for CODE in $(cut -c 7-21 Data.txt); do
    echo ">${CODE}_" > $header
    if [[ -r "${CODE}.fa" ]]; then
        sed '1d' "${CODE}.fa" > $scratch
        cat $header $scratch > ${CODE}_Edited.fa
    fi
done
  • Thank u Dope it Works like a charm. Could u pls explain briefly what the condition inside if loop? ^^" – Neuls Nov 29 '16 at 16:37
  • It's just a quick sanity check to make sure there's a readable file matching the pattern. – DopeGhoti Nov 29 '16 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.