Two variables in output file name nested for loops


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Two variables in output file name nested for loops
# 1  
Old 05-24-2012
Question Two variables in output file name nested for loops

I am trying to use two nested for loops to process some files and then create a new file using both variables in the output file name. I have several files in this naming style:
Code:
S1_L3_all_R1.fastq
S1_L3_all_R2.fastq
S1_L4_all_R1.fastq
S1_L4_all_R2.fastq
.
.
S1_L8_all_R1.fastq
S1_L8_all_R1.fastq

I need to apply a process to each file and rename the files like this:
Code:
S1_L3_all_R1.fasta
S1_L3_all_R2.fasta
.
.
S1_L8_all_R1.fasta
S1_L8_all_R2.fasta

I am using BASH. Here is what I have tried:

Code:
for((n=3;n<=8;n++))
do 
for((k=1;k<=2;k++))
do 
for f  in S1_L$n_all_filt_R*.fastq
do 
echo $f
cat $f | perl -e my file processing > S1_L$n_all_filt_R$k.fasta
done
done
done


This runs through the file processing for each file but the file name is S1_L1.fasta and this file name is recycled for each of my original files. It seems to me that everything after the first variable in my output file name is ignored.

How can I use two variables in a file name to get the output file names I am looking for?

Last edited by Scrutinizer; 05-24-2012 at 04:41 PM.. Reason: Removed times roman formatting
# 2  
Old 05-24-2012
You get a useless use of cat award Smilie

This is the problem:

Code:
$ n_=1234
$ echo $n_

1234

$

_ is being taken as part of the variable name here, and will do so inside strings too. To make it not do so, do ${n} instead of $n. I try to do this habitually, even.

In short:
Code:
for((n=3;n<=8;n++))
do 
        for((k=1;k<=2;k++))
        do 
                for f  in S1_L${n}_all_filt_R*.fastq
                do 
                        echo $f
                        perl -e my file processing <"$f" > S1_L${n}_all_filt_R${k}.fasta
                done
        done
done

Though I might just do:

Code:
for n in 3 4 5 6 7 8
do
        for k in 1 2
        do
        ...
        done
done

That way, this code could run in any Bourne shell, not just BASH.
# 3  
Old 05-24-2012
Corona,

Thank you for your reply. I knew that using cat in that way wasn't necessary but I don't have enough experience to know a better way to do what I needed to do. Your revisions work great! I did have to change the * that I was using to ${k}: the * was running the process on the correct file but was writing the output to the same file name (reusing file names).

I learned three things from you today; the use of {} with a variable, how to not use cat in this instance and an alternative way to acomplish the same thing.

Thanks!
# 4  
Old 05-24-2012
It's not a special case. Smilie Anywhere at all you have cat file | command, you can do command < file instead. You can even do so for entire shell statements, loops, and subshells the same way -- put the redirections on the end.

Code:
# This entire loop of code will have 'filename' as its standard input
while read LINE
do
...
done <filename

# This entire subshell will read from 'filename' as its standard input and write to 'outfile' as its standard output
# 'read' will read the first line.  Then 'echo' will write 'something' into 'outfile'.
# Then 'grep' will hunt for 'word' in the rest of the file and print any results after 'something' in 'outfile'.
( read FIRSTLINE ; echo "something" ; grep "word" ) <filename >outfile

The special case is whether you could do command filename instead of command <filename. Whether that works depends entirely on the program you're giving the filename to.

This is one of the big differences between the Bourne shell and the C shell, incidentally. Bourne lets you put complicated redirections wherever it's convenient. C shell only lets you put very simple redirections, in very specific places.

Last edited by Corona688; 05-24-2012 at 03:27 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Nested Loops for text file

Hi A text file containing data something likeVehicle: BMW Class checkin_note: Tyre : Four path_id : 11 vault_file_size: -1 Vehicle: Toyota Class checkin_note: Tyre : Four path_id : 11 vault_file_size: -1 Vehicle: Chevrolet Class checkin_note: Tyre : Five path_id :... (7 Replies)
Discussion started by: vipinHasija
7 Replies

2. Shell Programming and Scripting

two while nested loops

for server in $(echo `cat /tmp/ScanHosts_${USERSNAME}.TXT`) do for portnumber in $(echo `cat /tmp/ScanPorts_${USERSNAME}.TXT`) do #echo ${server} ${portnumber} ... (3 Replies)
Discussion started by: SkySmart
3 Replies

3. Shell Programming and Scripting

Nested for loops

Greetings All, The following script attempts to enumerate all users in all groups in the group file(GROUP) and echo the following information: GROUP ---> USER The script is as follows: IFS="," for GROUP in `ypcat -k group | cut -d" " -f1` do for USER in `ypcat -k group... (13 Replies)
Discussion started by: jacksolm
13 Replies

4. Shell Programming and Scripting

KSH nested loops?

KSH isn't my strong suit but it's what my company has to offer. I've got a script with two nested loops, a FOR and UNTIL, and that works fine. When I add a CASE into the mix I end up getting "Unexpected 'done' at line xx" errors. Any suggestions on this? for divi in at ce ci cm co de di fl... (9 Replies)
Discussion started by: mrice
9 Replies

5. Shell Programming and Scripting

Need help with Regular Expressions and nested loops

Ok... am going slightly loopy trying to get this working (no pun intended) What I need is to modify this code which takes a string input then echo's each character on a seperate line, to do the same thing but to put DIGIT: in front of numbers and LETTER: in front of letters. I know a regular... (5 Replies)
Discussion started by: U_C_Dispatj
5 Replies

6. Shell Programming and Scripting

Nested while loops (ksh scripting)

You can use one while inside another? I made the following script (without really knowing if I can use two while) to get 3 numbers different from each other at random: num1=$(( $RANDOM % 10 )) num2=$num1 while do num2=$(( $RANDOM % 10 )) done num3=$num1 while do while do... (1 Reply)
Discussion started by: ale.dle
1 Replies

7. Shell Programming and Scripting

nested for loops

I need help getting over this bump on how nested for loops work in shell. Say i was comparing files in a directory in any other language my for loop would look like so for(int i=0;to then end; i++) for(int y = i+1; to the end; y++) I can't seem to understand how i can translate that... (5 Replies)
Discussion started by: taiL
5 Replies

8. Shell Programming and Scripting

file reading in nested loops

I have to to read files simultaneously in two nested loops,but am getting error can anyone do the needful. useridFile=userIds.txt fname=kiran.txt exec<$useridFile while read line do echo "User IDs are..$line" USER_ID=$line REMOTE_DIR_LOCATION="/home/test/$USER_ID" SOURCE_DIR=$USER_ID... (1 Reply)
Discussion started by: KiranKumarKarre
1 Replies

9. Shell Programming and Scripting

Grepping within nested for loops

Good morning - I have publication lists from 34 different faculty members. I need to end up with the numbers of publications in common across all 34 faculty. I need to grep person1 (last name) in list2, person1 in list3, person1 in list 4, etc., then person2 in list3, person 2 in list4, etc.,... (2 Replies)
Discussion started by: Peggy White
2 Replies

10. Shell Programming and Scripting

Awk formatting of a data file - nested for loops?

Hello - is there any way in awk I can do... 4861 x(1) y(1) z(1) 4959 x(1) y(1) z(1) 5007 x(1) y(1) z(1) 4861 x(2) y(2) z(2) 4959 x(2) y(2) z(2) 5007 x(2) y(2) z(2) 4861 x(3) y(3) z(3) 4959 x(3) y(3) z(3) 5007 x(3) y(3) z(3) to become... 4861 x(1) y(1) z(1) 4861 x(2) y(2) z(2)... (3 Replies)
Discussion started by: catwoman
3 Replies
Login or Register to Ask a Question