Trouble Assigning AWK variables


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Trouble Assigning AWK variables
# 1  
Old 11-29-2011
Trouble Assigning AWK variables

Hi,

I made an executable file in terminal and it looks like this.
Code:
echo Enter the name of the file without the .wig extension
read NAME
echo Enter the ratio
read RATIO
awk '{$2*=$RATIO;{print $0}}' ${NAME}.wig > ${NAME}normalized.wig

I have a file with several million lines that look like this
Code:
variableStep chrom=Uextra span=25
201    1.0000
226    0.5556
251    0.2033
276    0.1238
301    0.1087
326    0.1238

What I am trying to do is ask for a file name, then ask for a ratio to normalize my data by, just a number like 0.5, by multiplying the second column of each line (not including headers) by 0.5 and replace that column with the new value and output that line into the normalized file. At the same time I do not want to do calculations on the headers (variableStep chrom=Uextra span=25), but I still want the headers in the output file. There are several headers which appear on lines much further into the files.

When I run the program it isn't giving me the values I expect, but it is making the new file. When I run the program removing the

echo Enter the ratio
read RATIO

and replacing $RATIO with the actual value I want, like 0.5, I get the values I expect.... but it is doing math on the headers and is removing the tabs and replacing them with spaces. So there are a few problems with my program.

Thanks!

---------- Post updated at 06:16 PM ---------- Previous update was at 05:54 PM ----------

!*!*!UPDATE!*!*!

I was able to fix the problem with the headers being calculated, using this

Code:
echo Enter the name of the file without the .wig extension
read NAME
awk '{if($1 == "variableStep") {print $0} else {$2*=.5952;{print $0}}}' ${NAME}.wig > ${NAME}normalized.wig

but I still can't seem to specify the RATIO variable to get it to multiply the second column by RATIO. I have to edit the program each time with the number I want (.5952)

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 12-01-2011 at 05:30 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 11-29-2011
If you are using a modern awk (nawk on a sun) then this will work:

Code:
read ratio
awk -v ratio=$ratio '{if($1 == "variableStep") {print $0} else {$2*=ratio;{print $0}}}' ${NAME}.wig > ${NAME}normalized.wig

And a suggestion to make it a bit easier to read:
Code:
awk -v ratio=$ratio '
{
  if( $1 != "variableStep") 
     $2 *= ratio;
  print;
}' ${NAME}.wig > ${NAME}normalized.wig

# 3  
Old 11-30-2011
Thanks

Thanks, the code you gave me worked.

Here is the final program looking to code anything like this.
Code:
echo Enter the name of the file without the .wig
read NAME
echo the ratio?
read ratio
awk -v ratio=$ratio '{if($1 == "variableStep") {print $0} else {$2*=ratio;{print $0}}}' ${NAME}.wig > ${NAME}normalized.wig


Last edited by Franklin52; 12-01-2011 at 05:30 AM.. Reason: Please use code tags for code and data samples, thank you
# 4  
Old 11-30-2011
You can simplify that a lot by knowing what statements before and after a code block do in awk.

Code:
awk -v ratio=$ratio '$1=="variableStep" { $2 *= ratio } 1

The $1=="variableStep" before the code block tells it to run { $2 *= ratio } only when $1=="variableStep".

The 1 after the code block tells it to print every single line (even when $2 *= ratio isn't run). You could put any logical statement there but we put '1' so it's always true.
# 5  
Old 12-05-2011
@Corona688 I can't seem to get it to work. It gives me this output.

Code:
variableStep 0 span=25
201     1.0000
226     0.5556
251     0.2033
276     0.1238
301     0.1087
326     0.1238
351     0.1799
376     0.2632
401     0.5000


I also noticed a problem with my previous program. The data it outputs is correct, but for some reason the new file has fields separated by spaces, not tab. I'm trying to figure out how to take this code.

Code:
echo Enter the name of the file without the .wig
read NAME
echo the ratio?
read ratio
awk -v ratio=$ratio '{if($1 == "variableStep") {print $0} else {$2*=ratio;{print $0}}}' ${NAME}.wig > ${NAME}normalized.wig

and have it make a new file with fields that are tab separated, not space.





---------- Post updated at 02:45 PM ---------- Previous update was at 02:40 PM ----------

@Corona668 I changed the == to != and it seems to be computing everything correctly now, but again its giving me a space separated set of fields and I need them to be tab separated.

Code:
variableStep chrom=Uextra span=25
201 0.5952
226 0.330693
251 0.121004
276 0.0736858
301 0.0646982
326 0.0736858
351 0.107076
376 0.156657


Last edited by radoulov; 12-05-2011 at 04:24 PM..
# 6  
Old 12-05-2011
Code:
awk -v OFS="\t" ...

# 7  
Old 12-05-2011
I fixed the problem with the space separated fields.

Code:
echo Enter the name of the file without the .wig
read NAME
echo the ratio?
read ratio
awk -v ratio=$ratio '{if($1 == "variableStep") {print $0} else { $2 *= ratio;{print $1 "\t" $2}}}' ${NAME}.wig > ${NAME}normalized.wig

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by radoulov; 12-05-2011 at 04:24 PM.. Reason: Code tags!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trouble Assigning Variable with Function

OSX 10.9 Good morning/afternoon/evening. I'm hoping to get some insight on assigning a variable when calling a function. The code below looks at my array and checks if the path exists. My actual code will have multiple arrays and I would like to define a specific array when I call the... (6 Replies)
Discussion started by: sudo
6 Replies

2. Shell Programming and Scripting

Assigning Variables

Hi, Can the below be clarified please. i just want to know what is the difference between the two ways of assigning variables as mentioned below. export SRC_TBL=${SRC_TBL-"MMA_COPAY_PLN_FACT_STG"} export SRC_TBL="MMA_COPAY_PLN_FACT_STG" thanks in advance :) Arun (1 Reply)
Discussion started by: Arun Mishra
1 Replies

3. Shell Programming and Scripting

Assigning variables

i have variables RECIPIENTS_DEVL,RECIPIENTS_UACC,RECIPIENTS_PROD i have a case statement to get the phase variable: case ${WMD_UPHASE1} in u) WMD_UPHASE4=UACC;; i) WMD_UPHASE4=DEVL;; p) WMD_UPHASE4=PROD;; d) WMD_UPHASE4=DEVL;; *) WMD_UPHASE4=DEVL;; esac I am unable to... (3 Replies)
Discussion started by: Arun Mishra
3 Replies

4. Red Hat

Trouble assigning user to group in OpenLDAP

Hello, I am working on setup LDAP Server and facing issue related to assigning user to a group. Below is the LDAP structure i am using. I have created Users,Groups and Servers ou's and sub ou's added to the same or Users as well as Groups OU. Logged in as: cn=Manager,dc=bebolabs,dc=net ... (0 Replies)
Discussion started by: sunnysthakur
0 Replies

5. Shell Programming and Scripting

Help with reading and assigning variables

Hi Gurus, I have a file named log with 2 lines Each line is a file name. eg $ cat log monday tuesday I need to read log and assign each output(filename) to a different variable. The following doesn't work:- while read A B do echo " a is ${A} " echo " b is ${B} " done <... (6 Replies)
Discussion started by: wisdom
6 Replies

6. UNIX for Dummies Questions & Answers

Assigning variables using awk

Hi, I am having a line which is separated by - I need to extract each field and put into some variable. a=`echo "this-is-the-case" | awk -F- '{print $1}' ` b=`echo "this-is-the-case" | awk -F- '{print $2}' ` c=`echo "this-is-the-case" | awk -F- '{print $3}' ` d=`echo "this-is-the-case" | awk... (2 Replies)
Discussion started by: posix
2 Replies

7. Shell Programming and Scripting

variables not assigning in a function

Hi GUYS, I have function. I am assigning a line count to count variable. But it is throwing an error at this line. function_recur (){ #file being created in this function lenth = `wc -l function_outpu.dat`; echo $lenth; } this is the error i got rec.ksh: lenth: not found. ... (3 Replies)
Discussion started by: mac4rfree
3 Replies

8. UNIX for Advanced & Expert Users

assigning variables to their defaults

Hi, Is there any way to assign defaults values to the shell variables without reassigning them ( restarting the session) for example after login the value of ORACLE_HOME=/a/b/c i have changed this value from the console export ORACLE_HOME=/c/d now what if i want the value exported to... (1 Reply)
Discussion started by: clx
1 Replies

9. Shell Programming and Scripting

assigning SED output to a variable = trouble!

i'm on a Mac running BSD unix. i have a script in which i ask the user to input the name of a mounted volume. i then call SED to substitute backslashes and spaces in place of the spaces. that looks like this: echo "Enter the name of the volume" read Volume echo "You've chosen \"$Volume\""... (7 Replies)
Discussion started by: hungryd
7 Replies

10. UNIX for Dummies Questions & Answers

assigning variables

Before I even attempt this, is it possible to grep for a pattern, maybe a partial sentence like "go to page 3", assign that to a variable and then use awk or something to pull out the 3 and assign it to a variable? So first I would have Gotopg = "go to page 3" then page = 3 (9 Replies)
Discussion started by: k@ssidy
9 Replies
Login or Register to Ask a Question