I have several files on a folder something like this:
If the file starts with 1a, I would like to add the following at the very beggining of the file
If the file starts with 1b then I should add the following
For files starting with 6
So on and so forth. Thus, at the end each file will have a reference sequence at the very top 'matching' the beggining of the file name.
Thanks in advance.
Assuming that you have files which contain the data that needs to be added to each file named after the reference. For example, file "1a.header" contains the header for reference id "1a".
function add_reference
{
typeset file=$1
typeset ref_id=${file%%-*}
typeset temp=${file}.tmp
cat ${ref_id}.header $file > $temp
mv $temp $file
}
for file in *.fas
do
add_reference $file
done
If you want to keep all the reference strings within your script (instead of external files), you can use associative arrays. Since, ksh does not support associative arrays, you will have to use either ksh93 or awk.
I am not big with awk, but the original code I posted can be modified to use ksh93 as follows:
#!/bin/ksh93
typeset -A reference_headers
##
## Add all the valid references in this associative array
##
reference_headers=(
[1a]=">Reference1a
AGCATGACGATCAGTGTGCGGGTGA"
[1b]=">Reference1b
AGCATGTGTGCGGGTGA"
);
function add_reference
{
typeset file=$1
typeset ref_id=${file%%-*}
typeset temp=${file}.tmp
(echo "${reference_headers[${ref_id}]}"; cat $file) > $temp
mv $temp $file
}
for file in *.fas
do
add_reference $file
done
Note that, on my Mac OS X, the default ksh shell itself is ksh93, but on lots of Unix systems there are separate executables for ksh and ksh93. If your system has only ksh, if the following command gives an output similar to what is shown, its ksh93, else not:
So long as the reference ids (1a, 1b, 6) are valid sh varname sequences (alphanumeric and underscore), and the replacement text does not contain a "." on a line by itself (this tells ed to exit insert mode):
#!/bin/sh
_r1a='>Reference1a
AGCATGACGATCAGTGTGCGGGTGA'
_r1b='>Reference1b
AGCATGTGTGCGGGTGA'
_r6='>Reference6
AGCATGTGTGCGGGTGATTTTTT'
for f in *.fas; do
printf '1i\n%s\n.\nw\nq\n' "$(eval echo \"\$_r"${f%%-*}"\")" | ed -s "$f"
done
#!/bin/sh
_r1a='>Reference1a
AGCATGACGATCAGTGTGCGGGTGA'
_r1b='>Reference1b
AGCATGTGTGCGGGTGA'
_r6='>Reference6
AGCATGTGTGCGGGTGATTTTTT'
for f in *.fas; do
printf '1i\n%s\n.\nw\nq\n' "$(eval echo \"\$_r"${f%%-*}"\")" | ed -s "$f"
done
I didn't try it on my test machine, but that error you are seeing means that illegal control character is appended to the end of the first line. Did you try using "dos2unix" on that file? As for the gedit.. Maybe you have character encoding misconfigured there? If it is set to something like DOS, then editor will insert those "^M" characters at the end of every line. Did you use gedit to write scripts earlier without problems?
#!/bin/sh$
$
_r1a='>Reference1a$
AGCATGACGATCAGTGTGCGGGTGA'$
$
_r1b='>Reference1b$
AGCATGTGTGCGGGTGA'$
$
_r6='>Reference6$
AGCATGTGTGCGGGTGATTTTTT'$
$
for f in *.fas$
do$
^Iprintf '1i\n%s\n.\nw\nq\n' "$(eval echo \"\$_r"${f%%-*}"\")" | ed -s "$f"$
done$
---------- Post updated at 05:16 PM ---------- Previous update was at 04:32 PM ----------
This does not look that good but it is getting the job done. Now the reference matches the file extension (Sample1.1a, Sample2.1b, Sample3, 1a, etc)
#!/bin/sh
for f in *.1a
do
printf ">Reference1a\nTTGATGTGCCAACTGCGCAAATAGCAGCAGCACTACCAGGACCTTCGCCCA\n" | cat - ${f} > Converted${f}
done
for i in *.1b
do
printf ">Reference1b\nTTGATGTGCCTACCAGGACCTTCGCCCA\n" | cat - ${i} > Converted${i}
done
for e in *.3
do
printf ">Reference3\nTTGATTCGCCCA\n" | cat - ${e} > Converted${e}
done