Visit The New, Modern Unix Linux Community


Issue with variable within a loop


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Issue with variable within a loop
# 1  
Issue with variable within a loop

I have this file (1.txt):
Code:
>sample
TCGGCCCGJOHNTTTTGCGGGCCGCATTGTCGCCAGGCGCDOEGGGGTTTGCGATCGCCACGGGGCTGATGGTGGCGACCCGCTGCACCCGG

I am using the following script to "trim" the sequence
Code:
awk '{gsub(/^.*JOHN|DOE.*$/,"",$0)} 1'

This is the output:
Code:
>sample
TTTTGCGGGCCGCATTGTCGCCAGGCGC

However, I do not get the expected result when I use the script above in a loop:
Code:
#!/bin/bash
Primers=( NULL "/^.*JOHN|DOE.*$/" )
for y in {1..1}
do
        echo "${Primers[$y]}"
        awk '{gsub("${Primers[$y]}","",$0)} 1' 1.txt
done

Output:
Code:
/^.*JOHN|DOE.*$/
>sample
TCGGCCCGJOHNTTTTGCGGGCCGCATTGTCGCCAGGCGCDOEGGGGTTTGCGATCGCCACGGGGCTGATGGTGGCGACCCGCTGCACCCGG

The script is echoing the right string /^.*JOHN|DOE.*$/ but the sequence is not being trimmed.
I do not get the expected output even if I store the string in a variable:
Code:
#!/bin/bash
Primers=( NULL "/^.*JOHN|DOE.*$/" )
for y in {1..1}
do
        echo "${Primers[$y]}"
        n=${Primers[$y]}
        awk '{gsub($n,"",$0)} 1' 1.txt
done

I really cannot see what I am doing wrong here
Any help will be greatly appreciated
PS. I am using the loop to process a bunch of different strings. My bash file will look something like this:
Code:
#!/bin/bash
Primers=( NULL "/^.*JOHN|DOE.*$/" "/^.*XXXX|YYYY.*$/" "/^.*ZZZZ|NNNN.*$/" "/^.*GGGGG|DDDD.*$/" )
for y in {1..4}
do
        echo "${Primers[$y]}"
        awk '{gsub("${Primers[$y]}","",$0)} 1' 1.txt
done


Last edited by Xterra; 03-22-2016 at 03:00 PM..
# 2  
Let's start by pointing out the way you can introduce shell variables to AWK.
Code:
# shell variable
var="value"

# pass $var to AWK and use
awk -v label="$var" '{print label}'

# 3  
Aia

I have tried that too:
Code:
#!/bin/bash
Primers=( NULL "/^.*JOHN|DOE.*$/" )
for y in {1..1}
do
	echo "${Primers[$y]}"
	n=${Primers[$y]}
	awk -v var="$n" '{gsub(var,"",$0)} 1' 1.txt
done

The output is still not correct:
Code:
/^.*JOHN|DOE.*$/
>sample
TCGGCCCGJOHNTTTTGCGGGCCGCATTGTCGCCAGGCGCDOEGGGGTTTGCGATCGCCACGGGGCTGATGGTGGCGACCCGCTGCACCCGG

# 4  
Try without the slashes:
Code:
Primers=( NULL "^.*JOHN|DOE.*$" )

Happens to not make a difference here, but it is better use single quotes:
Code:
Primers=( NULL '^.*JOHN|DOE.*$' )

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Scrutinizer

Awesome! That took care of it!

---------- Post updated at 07:13 PM ---------- Previous update was at 02:56 PM ----------

I am trying to understand why the slashes were the issue with the script.
would you mind elaborating a bit?
# 6  
The slashes are delimiters of a constant regular expression, like quotes are delimiters for a constant string. They are not part of the regular expression or the string.
Without the delimiters too many characters would conflict with the syntax.
Delimiters are not needed for variables - and indeed not allowed.
This User Gave Thanks to MadeInGermany For This Post:
# 7  
MadeInGermany

Thanks
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #531
Difficulty: Easy
Unicode is not backward compatible, so it does not work with plain ASCII characters.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

While Loop issue

Hi, i=0 t5=6000001 while do i=`expr $i + 1` t5=`expr $t5 + 1` echo $t5 done I am able to increment "col3" value but unable to get col1,col2 value. Input: t1=10001 t2=abc t3=ghkc (5 Replies)
Discussion started by: onesuri
5 Replies

2. Shell Programming and Scripting

Variable value substitution issue with awk command issue

Hi All, I am using the below script which has awk command, but it is not returing the expected result. can some pls help me to correct the command. The below script sample.ksh should give the result if the value of last 4 digits in the variable NM matches with the variable value DAT. The... (7 Replies)
Discussion started by: G.K.K
7 Replies

3. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

4. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

5. UNIX for Dummies Questions & Answers

Loop and variable not exactly variable: what's wrong

Hello guys, This truly is a newbie question. I'm trying to make a loop to execute simultaneous commands indefinitely while using variable. Here is how my mess looks like (this is just an example): #!/bin/bash IP=`shuf -n 1 IP.txt` # I figured this would be easier to select random lines... (4 Replies)
Discussion started by: bobylapointe
4 Replies

6. Shell Programming and Scripting

printing variable with variable suffix through loop

I have a group of variables myLINEcnt1 - myLINEcnt10. I'm trying to printout the values using a for loop. I am at the head banging stage since i'm sure it has to be a basic syntax issue that i can't figure out. For myIPgrp in 1 2 3 4 5 6 7 8 9 10; do here i want to output the value of... (4 Replies)
Discussion started by: oly_r
4 Replies

7. Shell Programming and Scripting

[SHELL: /bin/sh] For loop using variable variable names

Simple enough problem I think, I just can't seem to get it right. The below doesn't work as intended, it's just a function defined in a much larger script: CheckValues() { for field in \ Group_ID \ Group_Title \ Rule_ID \ Rule_Severity \ ... (2 Replies)
Discussion started by: Vryali
2 Replies

8. Shell Programming and Scripting

Issue with using While loop

Hi, I am trying to move a file from remote server to local server and when the transfer completes successfully i call a script in remote server to remove the file which was successfully transferred. I do this by first getting the list of file in remote server and move the text file to local... (8 Replies)
Discussion started by: funonnet
8 Replies

9. Shell Programming and Scripting

until loop issue.

Hi, my script is waiting for 3 files to come to a folder for 30 min but even when all the files arrive in the folder it's still waiting for these three files. Files can come with in 2 min and I want it to start processing them immediately after all the files arrive in the folder. until ; do... (3 Replies)
Discussion started by: gurpartap
3 Replies

10. Shell Programming and Scripting

Help With A For Loop Issue

I was wondering how I can modify this for loop, so it only loops through the filenames that do not have an ".old" extension. for filename in $(ls "$1") do echo $filename | grep '\.old$' > /dev/null if then mv $1/$filename $1/$filename.old fi done (5 Replies)
Discussion started by: ralts01
5 Replies

Featured Tech Videos