Question: How to assign the result of a command to a variable in shell script?
0
gravatar for dz2353
7 weeks ago by
dz235370
dz235370 wrote:

Hi, there

I want to assign the sample id to the output of trimmomatic but I failed. The name of the output is _clean.fastq and the variable j is missing. How can I figure out this issue? Thanks in advance!

for i in ./single/*.fastq
do
echo ${i}
j=$(echo ${i} | cut -d "." -f1)
java -jar /home/anaconda3/share/trimmomatic/trimmomatic.jar SE -phred33 ${i} ./single/${j}_clean.fastq ILLUMINACLIP:/home/anaconda3/share/trimmomatic/adapters/TruSeq3-SE.fa:2:30:10 SLIDINGWINDOW:4:15 LEADING:3 TRAILING:3 MINLEN:36
done
rna-seq • 175 views
ADD COMMENTlink written 7 weeks ago by dz235370

what are the names of the files, what are the errors, what are the messages, and what is the shell ?

ADD REPLYlink written 7 weeks ago by Pierre Lindenbaum120k

Actually, I just want to test if this shell script works. There is only one .fastq file in the ./single directory named SRR222423.fastq. The input of trimming is SRR222423.fastq. And I want to get the output in the same directory with the name like SRR222423_clean.fastq. No error appears. The output is a .fastq file with the name _clean.fastq. There is no sample id SRR222423. The shell is bash.

ADD REPLYlink written 7 weeks ago by dz235370
1

if your input is really ./single/*.fastq

then

echo ${i} | cut -d "." -f1

will be an empty string... because the first char is just a dot...

ADD REPLYlink written 7 weeks ago by Pierre Lindenbaum120k

Nope, I think the input is SRR222423.fastq and I split it by using dot. After that, I use -f1 to choose the first part then I get SRR222423. Is it correct?

ADD REPLYlink written 7 weeks ago by dz235370
3

If it generates expected output then it is correct. In this case. If you had more than one . in file names then you may get unexpected results. You could use basename instead.

ADD REPLYlink written 7 weeks ago by genomax68k
2

I’d even say you should always use basename for things like this.

ADD REPLYlink written 7 weeks ago by Michael Dondrup46k

The command basename works well, thanks so much!

ADD REPLYlink modified 7 weeks ago • written 7 weeks ago by dz235370
3
gravatar for Prakash
7 weeks ago by
Prakash1.2k
India
Prakash1.2k wrote:

I hope you got the answer as Pierre suggested. you have just splitted the string based on dot. so spliting it based on "/" and then "." would work

j=$(echo ${i} | cut -d "/" -f3 | cut -d "." -f1)
ADD COMMENTlink modified 7 weeks ago • written 7 weeks ago by Prakash1.2k

I tried but the output was {i}.fastq. The command basename provided by genomax works well. Thanks for your answer!

ADD REPLYlink written 7 weeks ago by dz235370
2

Then you would have more delimiter in you string. I tried to to reproduce similar to your input and it worked. yes basename is great idea indeed.

Screenshot-from-2019-04-30-09-24-13

ADD REPLYlink modified 7 weeks ago • written 7 weeks ago by Prakash1.2k

Thank u for your reply in detail and I make sense how does it work!

ADD REPLYlink written 6 weeks ago by dz235370
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1847 users visited in the last hour