Loops & Variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Loops & Variable
# 15  
Old 07-01-2015
Quote:
(yes, the values differ for each execution of the loop, but the schema stays the same). You could - if you don't want to convolute your "awk" statement too much, which is a good idea, always do one thing in a step, but do it good - put it in a variable:
Code:
 eval replacement=\${TMPArray${y}[$x]}
awk -v "REP=${replacement}"[...]

How you use the variable replacement in the script?
Code:
eval replacement="\${PrimerSet${PSet}[$oligo]}" | awk -v "REP=${replacement}" '{gsub("^.*" REP ".*$","",$0)} 1' $PSet.$oligo > $PSet.Result$oligo

the above does not work

Last edited by Xterra; 07-01-2015 at 01:46 PM..
# 16  
Old 07-01-2015
If you could tell us WHAT doesn't work? On first sight I can comment that you're using parentheses instead of braces for the REP assignment.
# 17  
Old 07-01-2015
Quote:
If you could tell us WHAT doesn't work? On first sight I can comment that you're using parentheses instead of braces for the REP assignment.
Sorry, I meant to use braces. Still, that does not work. The outfiles are empty which makes me think I am not assigning the variable replacement correctly.
# 18  
Old 07-01-2015
Quote:
Originally Posted by Xterra
How you use the variable replacement in the script?
Sigh. Exactly in the way i wrote it in the line immediately following the assignment and precisely in the way you quoted it.

Quote:
Originally Posted by Xterra
Code:
eval replacement="\${PrimerSet${PSet}[$oligo]}" | awk -v "REP=$(replacement)" '{gsub("^.*" REP ".*$","",$0)} 1' $PSet.$oligo > $PSet.Result$oligo

the above does not work
Yes. Because it is wrong for the reasons explained at length in my last post. I hate quoting myself but you probably overread the 12 paragraphs following

Quote:
First, the pipe is not necessary
You might want to read them now.

/PS: i just now saw you posted in the meantime and corrected the braces/parentheses confusion. If you think the variable is not correctly assigned why don't you just "echo" it?

Code:
[....]
    replacement=....
    echo $replacement
    [....]

This will immediately show you if the variable is correctly assigned or not, no?

I hope this helps.

bakunin

Last edited by bakunin; 07-01-2015 at 02:18 PM..
# 19  
Old 07-01-2015
Quote:
Originally Posted by Xterra
Sorry, I meant to use braces. Still, that does not work. The outfiles are empty which makes me think I am not assigning the variable replacement correctly.
This is exactly what I meant in my earlier post:

Quote:
Originally Posted by RudiC
.
.
.
Are you sure the gsub with the regex anchored on both ends will do what you want?
Rethink the use of gsub with that anchored regex.
# 20  
Old 07-01-2015
Maybe something like this?
Code:
#!/bin/bash
PrimerSet1=( NULL "GGGGGG|TTTTT" "AAAAAA|CCCCCC" "CCAAAA|CCCCAA" )
PrimerSet2=( NULL "XXXXX|YYYYYY" "ZZZZZZZ|BBBBBBB" "KKKKKK|LLLLLL" )
PrimerSet3=( NULL "SSSSS|FFFFFF" "EEEEEE|JJJJJJ" "PPPPPP|UUUUUU" )
for PSet in {1..3}
do
    for oligo in {1..3}
    do
        eval replacement=\${PrimerSet${PSet}[$oligo]}
        awk -v "REP=${replacement}" '{gsub("^.*" REP ".*$","",$0)} 1' $PSet.$oligo > $PSet.Result$oligo
    done
done

# 21  
Old 07-01-2015
Quote:
Originally Posted by Xterra
Maybe something like this?
Well, fame at last.

Yes, like this. Now, if you enter a "echo"line between the line starting with "eval" and the one starting with "awk" you can control easily what exactly is in the variable (and, if it is not to your liking, do something to correct it).

This is another advantage of well-written programs: they can be easily debugged if there is a suspicion they don't exactly do what they are supposed to.

If you now get rid of the "NULL" data it would be perfect.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash: How to use read with conditions & loops

Hello, Below I try to control that the input is good an IP : #!/bin/bash cp /home/scripts/choice_interfaces.txt /home/scripts/interfaces.txt chmod 644 /home/scripts/interfaces.txt echo -e "Please enter the network informations into the /etc/network/interfaces file, complete them below... (9 Replies)
Discussion started by: Arnaudh78
9 Replies

2. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

3. UNIX for Dummies Questions & Answers

Bash loops and variable scope

Hi All, I've been researching this problem and I am pretty sure that the issue is related to the while loop and the piping. There are plenty of other threads about this issue that recommend removing the pipe and using redirection. However, I haven't been able to get it working using the ssh and... (1 Reply)
Discussion started by: 1skydive
1 Replies

4. Shell Programming and Scripting

Re-assign variable's value through which FOR LOOP loops

Hi, I've a requirement where I want to re-assign the value in the variable through which FOR LOOP loops. For e.g. Snippet of code --------------- for i in $var do echo $i >> $tempFile var=`echo $another_var | awk -F" " '{print $1}'` done I am re-assigning var so... (2 Replies)
Discussion started by: dips_ag
2 Replies

5. Shell Programming and Scripting

sed -i '7 c\$variable' file ....(?!@#$%^&*!)

I have tried everything I can think of to get sed to change line N of a file to the contents of a variable. I have Googled the Internet, and I find lots of people telling how to use variables with the "Substitute" command, but no one telling how to use variables with the "Change" command. I... (4 Replies)
Discussion started by: Mr.Lauren
4 Replies

6. UNIX for Dummies Questions & Answers

Passing KSH variable to AWK with two loops

Hi, I need some help to figure out why an outer for loop KSH variable does not decode in AWK but inner for loop does. Below is my code, If I hard code variable 'SUBSEQ' in AWK it works but if I try to pass the SUBSEQ from KSH, it does not and when I pass the variable 'NAM' from KSH it works: I... (1 Reply)
Discussion started by: chowdhut
1 Replies

7. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

8. Shell Programming and Scripting

scripting headache... loops & variables

Surely there's an easier way to do this, lets see if anyone knows! I am new to scripting so go easy on me! I have the following script and at the moment it doesn't work and I believe the problem is that I am using a while loop within a while loop. When I run the script using sh -x I can see... (6 Replies)
Discussion started by: StevePace
6 Replies

9. Shell Programming and Scripting

search & replace in variable

Can I use search & replace in any variable? Suppose I have one variable named var1 which holds value "abcabc" I need to search 'a' in var1 and want to replace with 'x' like 'xbcxbc'. Is it possible? Can you provide me an example? Malay (3 Replies)
Discussion started by: malaymaru
3 Replies

10. Shell Programming and Scripting

korn shell "loops & arrays"

Hi, I am trying to write a script which will loop until a certain action has been performed. I have two files i would like to compares. For example: file1 has a list of user ids (about 900) from the company's e-mail server. file2 has a list of user ids (about 50 or so) from... (7 Replies)
Discussion started by: muzica
7 Replies
Login or Register to Ask a Question