Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 03-15-2012, 07:18 PM   #1
LQ Newbie
Registered: Mar 2012
Posts: 1

Rep: Reputation: Disabled
loading a variable with awk in a for loop

SO I am writing a script that needs to take a file as input that looks like:

GALNT10 5 153570295 153800543
KLHL32 6 97372496 97588630
FTO 16 53737875 54148379
MC4R 18 58038564 58040001
SEC16B 1 177897489 177939050
ADCY3 2 25042039 25142055
GNPDA2 4 44704168 44728612

and use that info to process another file and generate output that fall within the boundaries of START and END.

The script takes several arguments, where REGION is the file above, and FREQAA and FREQEA are additional files:


Everything was working fine until I put in this for loop:

for i in $(awk '{print $2}' $GENE.bim | head -$NR);do

EA1=$(grep -w -$i $FREQEA | awk '{print $3}')
EA2=$(grep -w -$i $FREQEA | awk '{print $4}')
AA1=$(grep -w -$i $FREQAA | awk '{print $3}')
AA2=$(grep -w -$i $FREQAA | awk '{print $4}')

in other similar lines earlier in the script, this type of thing works fine, but inside the for loop, instead of loading $EA1 with the third column of the line containing $i, it writes the value of the 3rd argument.
Old 03-16-2012, 05:48 AM   #2
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
I think you may have to explain further what you are attempting to do as the present code snippet makes no sense to me. Also, please use [code][/code] tags when
displaying code.

Maybe you could start by explaining where the NR variable comes from in the line below:
for i in $(awk '{print $2}' $GENE.bim | head -$NR);do
Old 03-17-2012, 12:50 PM   #3
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
To start with, Don't read lines with for. Use a while+read loop, with the awk command supplied by a process substitution (assuming bash, of course).

You shouldn't need to use head either. You can import the variable directly into awk and use it to output the lines you want.

while read i; do


done <( awk '( NR <= ln ) { print $2 }' "ln=$NR" "$GENE.bim" )
Edit: Similarly, you shouldn't need to use grep and awk (or grep and sed) together in the sub-commands.

EA1=$( awk '( $0 ~ pat ) { print $3 }' 'pat=\\<'"$i"'\\>' "$FREQEA" )
The "pat" variable in awk is treated as a regex to match, so that only lines containing the pattern will be printed. \< and \> are regex word boundary anchors, used to replicate the behavior of grep's "-w" option. Unfortunately though, awkward quoting and backslashing is needed to properly pass them to awk. If you don't need the whole-word matching condition, you can simply use "pat=$i".

Edit2: a slightly cleaner way to handle the regex, by adding the word boundries to the variable inside awk instead.
EA1=$( awk '{ pat="\\<" pat "\\>" } ; ( $0 ~ pat ) { print $3 }' "pat=$i" "$FREQEA" )

Speaking of which, QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS. You should never leave the quotes off a variable expansion unless you explicitly want the resulting string to be word-split by the shell. This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later.

Also, environment variables are generally all upper-case. So while not absolutely necessary, it's good practice to keep your own user variables in lower-case or mixed-case, to help differentiate them.

Last edited by David the H.; 03-17-2012 at 01:14 PM. Reason: as stated


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem while comparing awk field variable with input variable entered using keyboard vinay007 Programming 12 08-23-2011 01:44 AM
[SOLVED] [BASH] non-empty variable before loop end, is empty after exiting loop aitor Programming 2 08-26-2010 10:57 AM
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 03:18 AM
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 01:08 AM
awk in loop How to Nkunzis Linux - Newbie 3 12-10-2006 02:34 PM

All times are GMT -5. The time now is 08:46 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration