LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-28-2010, 02:12 AM   #1
bashprog
LQ Newbie
 
Registered: Sep 2010
Posts: 13

Rep: Reputation: 0
assigning column name to awk print in a loop


I am trying to assign column number/name to awk print option in a loop. But this is not working here. Can you help

Code:
#!/bin/sh


STR='overt 0 1 1.3 2 2.5 3 3.6 4 4.9 5 5.7 6 6.3'

for i in `seq 3 2 13`

do

#       echo "val:" ;
        echo i=$i
        val=\$$i
        echo val=$val 
        v1=`echo $STR | awk '{print $val;}'`
        echo $v1
done
I am trying to print values at col 3, 5, 7, 9, 11, 13 in example above, but this is not working. Any suggestions welcome. TIA.

Last edited by bashprog; 09-28-2010 at 02:13 AM.
 
Old 09-28-2010, 03:10 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
do it inside awk

Code:
echo "$STR" | awk '{for(i=3;i<=13;i+=2) print $i}'
there, short and sweet.
 
1 members found this post helpful.
Old 09-28-2010, 10:44 AM   #3
bashprog
LQ Newbie
 
Registered: Sep 2010
Posts: 13

Original Poster
Rep: Reputation: 0
That works, thanks. However I want to know why my code is not working. Also, since I am interested in extracting values from STR and using them as part of larger string , I want to use an outer loop. Can this be done ?
 
Old 09-28-2010, 08:34 PM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,473

Rep: Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888
I'll answer your question with a question, who's variable is $val in the following line:
Code:
v1=`echo $STR | awk '{print $val;}'`
By who I mean awk or bash? If you answer this you will have your answer, at least in part.

I will also assume that you were just using the echoes to check the data but are aware that the extra val variable is not required. At least
not if it is not to have any life past the awk.

Lastly, not understanding what the STR represents, have you thought of just putting it in an array and then using the appropriate indices? (ie no awk required at all)
 
Old 09-29-2010, 01:18 AM   #5
bashprog
LQ Newbie
 
Registered: Sep 2010
Posts: 13

Original Poster
Rep: Reputation: 0
This is what I was looking for:
Code:
#!/bin/sh


STR='abcdef 0 1 1.3 2 2.5 3 3.6 4 4.9 5 5.7 6 6.3'

for i in 3 5 7 9 11 13

do

        echo i=$i
        v1=`echo $STR | awk '{print $'$i';}'`
        echo v1=$v1
done
To answer your questions:
val is not awk's variable. I explored using arrays and that worked too. Thanks for your help!

Last edited by bashprog; 09-29-2010 at 10:45 AM.
 
Old 09-29-2010, 01:54 AM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,473

Rep: Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888
I am sure your seq will still work too. Another small piece of advice which is less obvious in a small example like this
but may later be helpful. Try and use awk's variables to encompass the bash ones as later it will get confusing if you have many
variables to change in and out of quotes.

For example:
Code:
#!/bin/sh


STR='overt 0 1 1.3 2 2.5 3 3.6 4 4.9 5 5.7 6 6.3'

for i in 3 5 7 9 11 13

do

        echo i=$i
        v1=`echo $STR | awk -v var=$i '{print $var}'`
        echo v1=$v1
done
 
  


Reply


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
awk multiple column into single column ilukacevic Programming 49 07-19-2010 07:23 PM
Assigning variables in a bash for loop JDska55 Linux - Newbie 6 06-18-2009 03:37 PM
Assigning variables by awk(?) bioinformatics_guy Linux - Newbie 2 02-19-2009 12:01 PM
Loop column output handband2 Programming 8 11-05-2008 03:40 PM
sed / awk command to print line number as column? johnpaulodonnell Linux - Newbie 2 01-22-2007 07:07 AM


All times are GMT -5. The time now is 01:26 PM.

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